【问题标题】:Convert raw images to bitmap in c#在c#中将原始图像转换为位图
【发布时间】:2013-07-30 20:42:10
【问题描述】:

我的代码目前如下所示:

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;
}

当我打开一个 RAW 图像时,程序停止并且 Visual Studio 说:

参数无效:Image raw = Image.FromStream(new MemoryStream(ba))

请帮忙!如何让 RAW 文件显示在 PictureBox 中?

【问题讨论】:

  • 您得到的确切错误是什么?运行时错误,编译错误...
  • ba 是 byte[] 而 MemoryStream 需要 ?嗯
  • GDI+ 不支持 RAW 格式。请参阅this question 了解建议的替代方法。

标签: c# image bitmap


【解决方案1】:

像这样创建位图:

Bitmap bmp = (Bitmap) Image.FromFile(open.FileName);

或者不使用位图:

 this.pictureBox1.Image = Image.FromFile(open.FileName);

示例 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);

【讨论】:

  • 我遇到了另一个异常“内存不足”。我做错了什么?if (fe == "CR2") { Bitmap bmp = (Bitmap)Image.FromFile(open.FileName); pictureBox1.Image = bmp; statusl.Text = fe; }
  • 图片尺寸是多少?
  • OutOfMemory 可能由以下原因引起:文件没有有效的图像格式。 -- GDI+不支持文件的像素格式。
  • 我尝试使用 .arw(Sony 原始图像)和 .cr2(Cannon 原始图像),它们是用相机拍摄的照片。
  • 图像不理解 .cr2 或 arw 文件。将它们转换为 .JPG 或 PNG / BMP 等。使用 WPF 可以使用 BitmapDecoder,并且需要安装支持 WIC 的编解码器
【解决方案2】:

您实际上是通过指定using (Image raw = Image.FromStream(new MemoryStream(ba))) 来处理图像,稍后将图像的Disposed 实例分配给picturebox,这会导致此异常。要使其正常工作,您必须不丢弃或克隆图像。

Bitmap raw = Image.FromStream(new MemoryStream(ba) as Bitmap;
pictureBox1.Image = raw;

或者干脆克隆

using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
    img = raw.Clone() as Bitmap;
}

以上两个都应该工作

【讨论】:

  • @user2635745 什么异常?更新堆栈跟踪和异常名称
  • "参数无效。"
【解决方案3】:

你试试这个代码:

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();
    }

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 2022-01-09
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多