在 C# 中将原始图像转换为位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17956960/
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
Convert raw images to bitmap in c#
提问by
My code currently looks like this:
我的代码目前看起来像这样:
if (fe == "CR2")
{
Image img = null;
byte[] ba = File.ReadAllBytes(open.FileName);
using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
img = raw;
}
Bitmap bm = new Bitmap(img);
pictureBox1.Image = bm;
statusl.Text = fe;
}
When I open a RAW image the program stops and Visual Studio says:
当我打开 RAW 图像时,程序停止并且 Visual Studio 说:
Parameter is not valid: Image raw = Image.FromStream(new MemoryStream(ba))
参数无效:Image raw = Image.FromStream(new MemoryStream(ba))
Please help! How can I get a RAW file to show in a PictureBox ?
请帮忙!如何让 RAW 文件显示在 PictureBox 中?
采纳答案by Martijn van Put
Create the bitmap like this:
像这样创建位图:
Bitmap bmp = (Bitmap) Image.FromFile(open.FileName);
or without using bitmap:
或不使用位图:
this.pictureBox1.Image = Image.FromFile(open.FileName);
Example WPF:
示例 WPF:
BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(origFile),
BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
BitmapEncoder bmpEnc = new BmpBitmapEncoder();
bmpEnc.Frames.Add(bmpDec.Frames[0]);
Stream ms = new MemoryStream();
bmpEnc.Save(ms);
Image srcImage = Bitmap.FromStream(ms);
回答by Sriram Sakthivel
You're actually disposing an Image by specifying using (Image raw = Image.FromStream(new MemoryStream(ba)))
later assigning the Disposed
instance of image to picturebox
which leads to this exception. To make to work you've to either don't dispose or clone the image.
您实际上是通过指定using (Image raw = Image.FromStream(new MemoryStream(ba)))
稍后分配导致此异常Disposed
的图像实例来处理图像picturebox
。为了工作,您必须不处理或克隆图像。
Bitmap raw = Image.FromStream(new MemoryStream(ba) as Bitmap;
pictureBox1.Image = raw;
Or simply Clone
或者干脆克隆
using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
img = raw.Clone() as Bitmap;
}
Both of the above should work
以上两个都应该工作
回答by MHAnbar
you try this code :
你试试这个代码:
private static void SaveImageToRawFile(string strDeviceName, Byte[] Image, int nImageSize)
{
string strFileName = strDeviceName;
strFileName += ".raw";
FileStream vFileStream = new FileStream(strFileName, FileMode.Create);
BinaryWriter vBinaryWriter = new BinaryWriter(vFileStream);
for (int vIndex = 0; vIndex < nImageSize; vIndex++)
{
vBinaryWriter.Write((byte)Image[vIndex]);
}
vBinaryWriter.Close();
vFileStream.Close();
}
private static void LoadRawFile(string strDeviceName, out Byte[] Buffer)
{
FileStream vFileStream = new FileStream(strDeviceName, FileMode.Open);
BinaryReader vBinaryReader = new BinaryReader(vFileStream);
Buffer = new Byte[vFileStream.Length];
Buffer = vBinaryReader.ReadBytes(Convert.ToInt32(vFileStream.Length));
vBinaryReader.Close();
vFileStream.Close();
}