图片框的字节数组 [英] Picture box to byte array

查看:181
本文介绍了图片框的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序选择图像,并设置在一个图片框,选择图像,然后转换成图像框字节数组,保存在图像类型列的SQL Server数据库中的图像。

I have a program to select an image and to set that selected image in a picture box then convert the image in the image box to byte array and save sql server data base in a image type column.

在浏览按钮单击事件,我选择这样的图像文件。

In browse button click event I'm selecting the image file like this.

OpenFileDialog fop = new OpenFileDialog();
fop.InitialDirectory = @"Desktop";
fop.Filter = "image files|*.jpg;*.png;*.gif";
if (fop.ShowDialog() == DialogResult.OK)
{
     FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);  
     pbPartySymbol.Image = new Bitmap(fop.FileName);
     MessageBox.Show("Image Imported Successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}



选择图像并将其设置图片框图像之后,我转换图片框图像的保存按钮单击事件字节数组保存字节数组数据库。

After selecting the image and setting it the picture box image I'm converting the picture box image to byte array in the save button click event and saving the byte array to database.

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}



我打电话这样的方法。

I'm calling the method like this.

byte[] myArr1 = imageToByteArray(pbPartySymbol.Image);

和我通过这个字节数组到数据库。它可以节省了。但是的所有的附加图像保存为这样。* 0x53797374656D2E427974655B5D *保存的图像无法回到在读操作中的图片框。我是什么做的不对的保存的?

and I'm passing this byte array to the data base. and it saves too. But ALL the added images are saved as like this.* 0x53797374656D2E427974655B5D* Saved images cannot be get back to the picture box in the reading operation. What am I doing wrong in the SAVING?

下面是我在拯救的形式配合操作。

Here's what I do in saving operation in the form.

            Party ptObj = new Party(myArr1);
            if (new PartyOP().saveParty(ptObj))
            {
                MessageBox.Show("NEW data added");
            }

在我的经营层,这是我的代码。

In my business operation layer this is my code.

          public Boolean saveParty(Party ptObj)
         {
          string query1 = "EXEC insertToParty'" + ptObj.PTSymARR + "'";
           return (new DataAccessLayer().executeNonQueries(query1));
        }

下面是我如何设定在党的阶级属性。

Here's how I have set the property in the Party class.

class Party
{
    public Party() { }
    public Party(byte[] ptSym) 
    {

        this._PTSymARR = ptSym;

    }
    public byte[] PTSymARR
    {
        get { return _PTSymARR; }
        set { _PTSymARR = value; }
    }


}



这是我的存储过程。
CREATE PROCEDURE insertToParty

@ptSymbol图像

AS
BEGIN
BEGIN TRANSACTION
SET NOCOUNT ON;
--query
INSERT INTO党(PTSYM)
值(@ptSymbol);

here's my stored procedure. CREATE PROCEDURE insertToParty ( @ptSymbol image ) AS BEGIN BEGIN TRANSACTION SET NOCOUNT ON; --Query INSERT INTO Party(PTSYM) VALUES (@ptSymbol);

PTSYM的数据类型是图像数据类型。

Data type of PTSYM is image data type.

推荐答案

这是十六进制字符串tranlates到 System.Byte []

That Hex string tranlates to System.Byte[].

您的字节数组添加到字符串开头 EXEC insertToParty 这样的.Net假设您想要的字符串表示该变量。除非另有说明它调用的ToString至极给你......System.Byte []

You add the Byte-Array to the string starting with EXEC insertToParty so .Net assumes you want a string representation of that variable. Unless specified it calls .ToString wich gives you .... "System.Byte[]".

编辑:您的存储过程不上插入使用十六进制编码。 图片数据显示,十六进制在SQL工作室等等看。

Your stored procedure does not use hex encoding on insert. image data shows in hex when viewed in SQL Studio etc.

如果您将您的字节数组编码为一个字符串使用Base64或十六进制插入和解码,当你读出的数据,你只需要担心增加了存储的时候,但我建议你从动态SELECT改变你的数据库的存取权限准备好的语句与参数:

If you would encode your byte array to a string using base64 or hex when inserting and decode that when you read the data you only had to worry about the increased storage, but I recommend that you change your db acces from a dynamic SELECT to a prepared statement with parameters:

SqlCommand command = new SqlCommand("Exec InsertToParty(@blob)");
command.Parameters.AddWithValue("@blob", ptObj.PTSymARR);
command.ExecuteNonQuery();

这篇关于图片框的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆