Bitmap.Save vs File.WriteAllBytes [英] Bitmap.Save vs File.WriteAllBytes

查看:112
本文介绍了Bitmap.Save vs File.WriteAllBytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组(imgBuffer),它包含jpeg图像的rgb值。我想将它保存到位图。当我使用 System.IO.File.WriteAllBytes(fileImg.jpg,imgBuffer); 我能够以正确的格式在我的目录中看到图像。当我尝试将相同的字节数组imgBuffer写入Bitmap对象时,图像被破坏。 (图像格式为24bpp,320x240)

  BitmapData数据; 
位图myBitmap =新位图(320,240,PixelFormat.Format24bppRgb);
//将字节转换为位图
data = myBitmap .LockBits(new Rectangle(Point.Empty,bitmapResized.Size),ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
Marshal.Copy(imgBuffer,0,data.Scan0,imgBuffer.Length);
myBitmap .UnlockBits(data);
myBitmap .Save(bitmapImg.jpg);

必须更改什么才能将bitmapImg正确输出到我的目录中?我目前收到以下损坏的图像。



$如果您可以将 imgBuffer 字节数组保存到磁盘并将其作为JPEG格式读取一个图像查看器,那么它不是图像的原始RGB值。



您可以从>加载位图一个流并使用 imgBuffer 中的数据作为源,并且您将得到一个位图对象,您可以使用它。


I have a byte array (imgBuffer) which contains the rgb values for a jpeg image. I would like to save this to a Bitmap. When I use System.IO.File.WriteAllBytes("fileImg.jpg", imgBuffer); I am able see the image in my directory in its correct format. The image gets corrupted when I try to write the same byte array, imgBuffer, to a Bitmap object. (The image format is 24bpp, 320x240)

BitmapData data;
Bitmap myBitmap = new Bitmap(320, 240, PixelFormat.Format24bppRgb);
//convert bytes to a bitmap
data = myBitmap .LockBits(new Rectangle(Point.Empty, bitmapResized.Size), ImageLockMode.WriteOnly,
                                  PixelFormat.Format24bppRgb);
Marshal.Copy(imgBuffer, 0, data.Scan0, imgBuffer.Length);
myBitmap .UnlockBits(data);
myBitmap .Save("bitmapImg.jpg");

What must I change so that bitmapImg is correctly outputted to my directory? I am currently getting the following corrupted image.

解决方案

If you can save your imgBuffer byte array to disk and read it as a JPEG in an image viewer, then it is not the raw RGB values for the image.

You can load a Bitmap from a stream and use the data in imgBuffer as your source, and you will get a Bitmap object back that you can use.

这篇关于Bitmap.Save vs File.WriteAllBytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆