簡體   English   中英

將字節數組轉換為圖像時,參數無效

[英]Parameter is not valid , while converting byte array to image

我想在訪問數據庫中保存圖像,我使用OLE對象。

我們的想法是將圖像轉換為字節數組,然后在數據庫中添加bytes數組。

這是功能:

    public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }

這很好用。

當我想將bytes數組返回給圖像時,我得到一個異常:

參數異常未處理參數無效。

我嘗試了兩個函數來將bytes數組轉換為image:

    public static Image ImageFromByte(byte[] image)
    {
        ImageConverter ic = new ImageConverter();
        Image img = (Image)ic.ConvertFrom(image);//here the exception comes
        return img;
    }

要么:

    public static Image ImageFromByte1(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);//here the exception comes
        return returnImage;
    }

有什么問題,如何解決?

嘗試將圖像保存到磁盤,看看是否有效。 根據需要更改文件名和擴展名。

這些方面的東西:

            string path = @"c:\myimage.jpg";
            using (MemoryStream inputStream = new MemoryStream(byteArrayIn))
            {
                using (Stream file = File.Create(path))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int len;
                    while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        file.Write(buffer, 0, len);
                    }
                } 
            }

編輯:將圖像寫入磁盤工作,我想你可以看到圖像。 試試這個:

            using (MemoryStream inputStream = new MemoryStream(byteArrayIn))
            {
                using (var image = Image.FromStream(inputStream))
                {
                    // see if this works.
                    // handle the image as you wish, return it, process it or something else.
                } 
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM