如何将图像转换为字节[] ............... [英] How to Convert Image in to the byte[]...............
本文介绍了如何将图像转换为字节[] ...............的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有Image并且我已经转换为字节...与notpad中可见的图像相同...我想要.......
public byte [] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
我知道但如何在上面的代码中传递图片,无法理解该代码,任何人都可以帮助我....
解决方案
尝试将图像转换为byte []:
public byte [] ConvertImageToByteArray(Image imageIn)
{
var memoryStream = new MemoryStream();
imageIn.Save(memoryStream,ImageFormat.Jpeg);
memoryStream.Close();
return memoryStream.ToArray();
}
如果需要,可将其转换回图片:
public Image ConvertByteArrayToImage( byte [] byteArrayIn )
{
var memoryStream = new MemoryStream(byteArrayIn);
var returnImage = Image.FromStream(memoryStream);
memoryStream.Close();
return returnImage;
}
希望有所帮助:)
一旦我想要功能性模拟器你的问题和我做过类似的事情。
这不是你问题的确切解决方案,但它可能会给你一个想法。
< pre lang =c#> private void btnBrowse_Click( object sender,EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = 选择你的文件;
if (ofd.ShowDialog()== DialogResult.OK)
{
filepath = ofd.FileName。的ToString();
}
long size =( new FileInfo(filepath).Length);
FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.ReadWrite);
byte [] data = new byte < /跨度> [大小];
尝试
{
fs.Read(data, 0 ,( int )size);
// 这个来自FileStream的读取流fs并写入字节数组数据。
// 在这里获取data []字节数组。现在您可以根据您的要求使用它
}
catch (Exception ex)
{
MessageBox.Show( 文件读取错误... + ex.ToString());
}
最后
{
fs.Close();
fs.Dispose();
}
MemoryStream ms = new MemoryStream();
ms.Write(data, 0 ,( int )size);
// 您可以在这里获取新文件并将此内存流添加到其中。
destPath = Application.StartupPath.ToString()+ \\ images;
destPath = Path.Combine(destPath,Path.GetFileName(filepath));
File.Copy(filepath,destPath);
}
希望这个帮助
I have Image and I have convert into the byte... same as Image visible in notpad...that i want.......
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
that i know but how to pass image in above code,can't understand that code,,can anyone help me please....
解决方案
Try this to convert image to byte[]:
public byte[] ConvertImageToByteArray(Image imageIn) { var memoryStream = new MemoryStream(); imageIn.Save(memoryStream, ImageFormat.Jpeg); memoryStream.Close(); return memoryStream.ToArray(); }
And this to convert it back to Image if you need:
public Image ConvertByteArrayToImage(byte[] byteArrayIn) { var memoryStream = new MemoryStream(byteArrayIn); var returnImage = Image.FromStream(memoryStream); memoryStream.Close(); return returnImage; }
Hope it helps :)
Once i wanted functionality similler to your problem and i have done something like this.
This is not exact solution to your problem but it may give you an idea.
private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "choose your file"; if (ofd.ShowDialog() == DialogResult.OK) { filepath = ofd.FileName.ToString(); } long size = (new FileInfo(filepath).Length); FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite); byte[] data = new byte[size]; try { fs.Read(data, 0, (int)size); //This read stream from FileStream fs and write to byte array data. //Here you Get the data[] byte array. now you can use it as per your requirement } catch (Exception ex) { MessageBox.Show("error in file read..." + ex.ToString()); } finally { fs.Close(); fs.Dispose(); } MemoryStream ms = new MemoryStream(); ms.Write(data, 0, (int)size); //Here you can take new file and add this memory stream to it. destPath = Application.StartupPath.ToString() + "\\images"; destPath = Path.Combine(destPath, Path.GetFileName(filepath)); File.Copy(filepath, destPath); }
Hope This help
这篇关于如何将图像转换为字节[] ...............的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文