使用 C# 更改位图

发布于 2024-11-07 00:53:19 字数 1234 浏览 0 评论 0原文

我正在做我的大学项目,我试图改变位图的位值。

我将位图加载到内存流,然后将其提取到 byte[] 数组。现在我正在更改这个 byte[] 数组的几个中心位,然后将其转换回位图图像。

但我遇到了“无效位图”的运行时异常。
位图是否有一些特殊的格式而不是简单的位???

以下是我使用的代码:

MemoryStream mstream = new MemoryStream();
        Bitmap b = new Bitmap(@"D:\my_pic.bmp");
        b.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
        byte[] ba = mstream.ToArray();
        mstream.Close();

        byte[] _Buffer = null;

        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);


        System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);


        long _TotalBytes = new System.IO.FileInfo(_FileName).Length;


        _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);

        // close file reader
        _FileStream.Close();
        _FileStream.Dispose();
        _BinaryReader.Close();
        int leng1 = ba.Length;
        int leng2=_Buffer.Length;

for(i=(leng1)/2;i<leng1;i++)
{ 
 for(j=0;j<leng2;j++)
{
ba[i]=_Buffer[j];
}
if(j==(leng2-1))
{
 break;
 }
  }
   TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
   Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(ba);

I was working on my college project where i was trying to change bit values of a Bitmap.

I am loading the bitmap to the memory stream, then extracting it to byte[] array. Now I was changing few central bits of this byte[] array and then converting it back to a bitmap image.

But i got a run time exception that "Invalid Bitmap".
Does a bitmap have some special format instead of simple bits???

Following is the code used by me:

MemoryStream mstream = new MemoryStream();
        Bitmap b = new Bitmap(@"D:\my_pic.bmp");
        b.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
        byte[] ba = mstream.ToArray();
        mstream.Close();

        byte[] _Buffer = null;

        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);


        System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);


        long _TotalBytes = new System.IO.FileInfo(_FileName).Length;


        _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);

        // close file reader
        _FileStream.Close();
        _FileStream.Dispose();
        _BinaryReader.Close();
        int leng1 = ba.Length;
        int leng2=_Buffer.Length;

for(i=(leng1)/2;i<leng1;i++)
{ 
 for(j=0;j<leng2;j++)
{
ba[i]=_Buffer[j];
}
if(j==(leng2-1))
{
 break;
 }
  }
   TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
   Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(ba);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-11-14 00:53:19

您必须有自己的目标,想要在如此低的级别上使用位图进行操作,这很好。除非您受到性能限制,否则在图形 API 级别上绘制图形方式会更容易。即使您对性能敏感,其他人也已经在丛林中开辟了一条道路。

回到问题。 BMP 文件格式比其他一些文件格式更简单,但仍然有多种类型。这里详细介绍了 BMP 格式的血腥细节:

现在,如果您是只需解析您自己的 BMP 文件,您就知道它们是 32 位 RGB,并且标题将是这样那样的大小,您可以跳过等等,那么这可能对您有用。如果您需要处理任何旧的 BMP,它很快就会变得混乱,这就是为什么图书馆会尽力为您处理一切。

You must have your own goal to want to operate at this low level with a bitmap and that's fine. Unless you are performance bound it is way easier to do graphics at the graphics API level. Even if you are performance sensitive, other people have cut a path through the jungle already.

Back to the question. The BMP file format is simpler than some others but still comes in many varieties. Here is a detailed introduction to the gory details of the BMP format:

Now if you are just parsing your own BMP files and you know they are 32-bit RGB, and the header is going to be such-and-such a size which you can skip over, etc, then that might work for you. If you need to handle any old BMP it gets messy real fast which is why the libraries try to take care of everything for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文