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
谢谢
6 个解决方案
#1
8
try this
试试这个
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);
return img;
}
#2
2
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;
#3
0
try this,
试试这个,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
#4
0
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);
#5
0
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"];
}
#6
0
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);
}
#1
8
try this
试试这个
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);
return img;
}
#2
2
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;
#3
0
try this,
试试这个,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
#4
0
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);
#5
0
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"];
}
#6
0
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);
}