【问题标题】:How can I read a byte[] out of the database and convert it to an image?如何从数据库中读取 byte[] 并将其转换为图像?
【发布时间】:2010-11-24 08:49:46
【问题描述】:

我有一个返回 varbinary(max) 类型数据的存储过程。我想将该数据转换为图像。

但我对这一行有问题:

public Image CargarAvatar(string login)
        {
            System.Object[] Args = new System.Object[1];
            Args[0] = login;

            DataTable X = new DataTable();
            X = TraerDataTable("sp_CargarAvatarUsuario", Args);

            byte[] ImagemByte = Convert.to (X.Rows[0][0].ToString());


            MemoryStream ms = new MemoryStream();
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

请帮忙! :D

【问题讨论】:

    标签: c# image bytearray


    【解决方案1】:

    varbinary 字段以字节数组的形式返回,因此您只需对其进行强制转换:

    byte[] ImagemByte = (byte[])X.Rows[0][0];
    

    然后你使用数组来创建内存流:

    MemoryStream ms = new MemoryStream(ImagemByte);
    

    【讨论】:

    • 经过不懈的努力,我所要做的就是将字节数组传入流中。 +1!
    【解决方案2】:

    在这种情况下,我不确定是否将 Int 转换为 byte[],因为我在任何地方都看不到 int。这当然可以,我只是在这种情况下没有看到应用程序。

    你有两个真正的问题。一个是创建 byte[],显然你知道它不会编译。第二个是将这些字节放入流中。

    public Image CargarAvatar(string login)
    {
        System.Object[] Args = new System.Object[1];
        Args[0] = login;
    
        DataTable X = TraerDataTable("sp_CargarAvatarUsuario", Args); // No need to create a new DataTable and overwrite it with the return value of TraerDataTable. One assignment will do.
    
        byte[] ImagemByte = (byte[])X.Rows[0][0]; // If this is an Image in the database, then this is already a byte[] here and just needs to be casted like so.             
    
        MemoryStream ms = new MemoryStream(ImagemByte); // You need to pass the existing byte[] into the constructor of the stream so that it goes against the correct data
        Image returnImage = Image.FromStream(ms);
    
        return returnImage;
    }
    

    【讨论】:

      【解决方案3】:

      您正在尝试从空的内存流创建图像。将字节数组作为参数传递给 MemoryStream 构造函数:

      MemoryStream ms = new MemoryStream(ImagemByte);
      

      【讨论】:

        【解决方案4】:

        这用于将图像转换为字节数组,可以按原样插入数据库:

        public byte[] imageToByteArray(BitmapImage myimage)
            {
                MemoryStream ms = new MemoryStream();
                WriteableBitmap wb = new WriteableBitmap(myimage);
                wb.SaveJpeg(ms, myimage.PixelWidth, myimage.PixelHeight, 0, 100);
                byte[] imageBytes = ms.ToArray();
                return imageBytes;
            }
        

        这是后路:

        public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
            {
                MemoryStream stream = new MemoryStream(byteArray);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(stream);
                return bitmapImage;
            }
        

        【讨论】:

          猜你喜欢
          • 2020-12-02
          • 2021-04-07
          • 1970-01-01
          • 2020-09-02
          • 2017-05-04
          • 1970-01-01
          • 1970-01-01
          • 2021-09-22
          • 1970-01-01
          相关资源
          最近更新 更多