【问题标题】:How to read .img file in C#如何在 C# 中读取 .img 文件
【发布时间】:2018-03-30 00:46:01
【问题描述】:

我正在开发一个 Windows 窗体应用程序,其中我需要做的一件事是从 .img 文件中提取图像。我能够读取普通的 jpg 和 png 文件,但不能读取 .img 文件。

我在互联网上找不到太多关于此的信息。我确实在 msdn 上找到了一些代码,并试图让它工作。下面是抛出的代码和异常。

 FileInfo file = new FileInfo(FilePath.Text);
 FileStream f1 = new FileStream(FilePath.Text, FileMode.Open, 
 FileAccess.Read, FileShare.Read);
 byte[] BytesOfPic = new byte[Convert.ToInt32(file.Length)];
 f1.Read(BytesOfPic, 0, Convert.ToInt32(file.Length));

 MemoryStream mStream = new MemoryStream();
 mStream.Write(BytesOfPic, 0, Convert.ToInt32(BytesOfPic.Length));
 Bitmap bm = new Bitmap(mStream, false);
 mStream.Dispose();

 // ImageBox is name of a PictureBox
 ImageBox.image = bm;   // this line is throwing the error

捕捉到异常

System.ArgumentException:参数无效。 在 System.Drawing.Bitmap..ctor(流流,布尔值 useIcm) 在 C:\Users\tiwar\Desktop\A02-Stegnography\A02-Stegnography\Form1.cs:line 65 中的 A02_Stegnography.Form1.ReadImgFile() 处

如果这是一个愚蠢的问题,我很抱歉。我希望我已经提供了足够的信息,但如果我还没有,请告诉我。

【问题讨论】:

  • img 不是你可以用内存流打开的图像文件
  • 是的,这就是为什么我用文件流打开它,在字节数组中读取它,然后用 MemoryStream 将它写入位图。
  • ,哎呀,没看到!
  • 该错误意味着您的流为空
  • img文件是什么图片? System.Drawing.Bitmap 仅支持certain file formats

标签: c# .net winforms bitmap picturebox


【解决方案1】:
FileInfo file = new FileInfo(FilePath.Text);
FileStream f1 = new FileStream(FilePath.Text, FileMode.Open, 
FileAccess.Read, FileShare.Read);
byte[] BytesOfPic = new byte[Convert.ToInt32(file.Length)];
f1.Read(BytesOfPic, 0, Convert.ToInt32(file.Length));

using (MemoryStream mStream = new MemoryStream())
{
    mStream.Write(BytesOfPic, 0, BytesOfPic.Length);
    mStream.Seek(0, SeekOrigin.Begin);
    Bitmap bm = new Bitmap(mStream);

    // ImageBox is name of a PictureBox
    ImageBox.image = bm;
}

你可以试试我的解决方案

【讨论】:

  • 您好 Chaunv,我尝试了您的解决方案,但仍然遇到相同的错误。顺便说一句,我刚用软件打开了.img文件,结果里面有两张.jpg图片,不是一张。我不知道这是否重要,只是想让你知道。
  • 如果您的 .img 文件包含 2 个文件 .jpg 则处理会出现错误,因为我们将 1 个图像加载到 1 个 Picturebox 中。您需要获取 2 个文件 .jpg 并加载到 2 个 Picturebox 中。
  • 我会看看我是否能找到一些解决方案,如果我没有,我可能会使用 7-zip 从外部获取图像。也会接受你的回答。
猜你喜欢
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2021-07-06
  • 1970-01-01
相关资源
最近更新 更多