4

我的代码目前如下所示:

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 中?

4

3 回答 3

5

像这样创建位图:

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);
于 2013-07-30T20:50:19.190 回答
3

您实际上是通过指定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;
}

以上两个都应该工作

于 2013-07-30T20:53:35.887 回答
1

你试试这个代码:

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();
    }
于 2017-09-05T14:59:46.197 回答