C# 将字节转换为图像时出现错误“参数无效”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17667818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:09:06  来源:igfitidea点击:

Error" Parameter is not valid " while converting Bytes into Image

c#.netimagebytearraymemorystream

提问by Umair Aslam

I am converting bytes into an image but I get an error

我正在将字节转换为图像,但出现错误

Parameter is not valid

参数无效

I am pasting my code. Kindly check the code and suggested that was I am doing right or wrong.

我正在粘贴我的代码。请检查代码并建议我做对还是错。

Image arr1 = byteArrayToImage(Bytess);

This is the function.

这就是功能。

public static Image byteArrayToImage(byte[] byteArrayIn)
{
        if (null == byteArrayIn || byteArrayIn.Length == 0)
            return null;

        MemoryStream ms = new MemoryStream(byteArrayIn);
        try
          {
            Process currentProcess1 = Process.GetCurrentProcess();
            Image returnImage = Image.FromStream(ms);
            return returnImage;
          }
        catch (Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
    }

I applied many techniques and solutions but it did not work for me

我应用了许多技术和解决方案,但对我不起作用

Your answer would be appreciated.

您的回答将不胜感激。

Thanks

谢谢

采纳答案by sangram parmar

try this

尝试这个

public Image byteArrayToImage(byte[] byteArrayIn)
{
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    Image img = (Image)converter.ConvertFrom(byteArrayIn);

    return img;
}

回答by Mohan Gopi

try this,

尝试这个,

public Image byteArrayToImage(byte[] byteArrayIn)
{
     Image returnImage = null;
     using (MemoryStream ms = new MemoryStream(byteArrayIn))    
     {   
         returnImage = Image.FromStream(ms);     
     }
     return returnImage;
}

回答by Ertyui

After trying many things I found a way which has a little bit more control. In this example you can specify the pixel format and copy the bytes to a Bitmap.

在尝试了很多事情之后,我找到了一种可以控制更多的方法。在此示例中,您可以指定像素格式并将字节复制到位图。

byte[] buffer = GetImageBytes();
var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length);
bitmap.UnlockBits(bitmap_data);
var result = bitmap as Image;

回答by Ankush Singhal

cmd.CommandText="SELECT * FROM `form_backimg` WHERE ACTIVE=1";

MySqlDataReader reader6= cmd.ExecuteReader();

if(reader6.Read())
{
   code4 = (byte[])reader6["BACK_IMG"];   //BLOB FIELD NAME BACK_IMG
}
reader6.Close();

MemoryStream stream = new MemoryStream(code4);   //code4 is a public byte[] defined on top                             
pictureBox3.Image = Image.FromStream(stream);

回答by user8250086

The problem is because, you are bringing it incorrectly from database. Try changing your code like this:

问题是因为,您从数据库中错误地获取了它。尝试像这样更改您的代码:

while (registry.Read())
{
   byte[] image = (byte[])registry["Image"];
}

回答by Quantumleapr

In my case I got the error since my base64 string had wrong encoding before calling Image.FromStream. This worked for me in the end:

就我而言,我收到错误,因为我的 base64 字符串在调用 Image.FromStream 之前编码错误。这最终对我有用:

byte[] bytes = System.Convert.FromBase64String(base64ImageString);

using (MemoryStream ms = new MemoryStream(bytes))
{
    var image = Image.FromStream(ms);
    image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}