在C#中使用二进制读取器读取位图文件 [英] Read bitmap file using binary reader in c#

查看:136
本文介绍了在C#中使用二进制读取器读取位图文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个使用位图类显示位图图像的程序,但是我的实际要求是使用二进制阅读器显示位图图像,然后将其转换为灰度...请帮助我.
我只是不想使用位图类,想通过使用Binary Reader来完成任务,并且在使用Binary Reader读取图像后,我要求将其转换为GreyScale,现在我希望我已经阐明了我的问题.帮助! !!

I have made a program which displays a bitmap image by using bitmap class.But my actual requirement is to display a bitmap image using binary reader and then convert it into grayscale...please help me out in this.
I just d''not want to use bitmap class,want to accomplish the task by using Binary Reader and after reading the Image using Binary Reader, i require it to be converted into GreyScale, now i hope i have clarified my question.Help!!!

推荐答案

如果您使用二进制读取器读取位图(即作为原始数据字节),然后尝试手动将其转换为灰度,则必须继续阅读并实现位图文件格式-可能要比将图像转换为灰度级还要多得多.

此处提供了有关如何进行此类转换的代码的教程: http: //www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale [
If you read a bitmap with a binary reader (i.e. as raw data bytes) and then try to convert it to grayscale manually, then you will have to read up on and implement Bitmap file format - probably a lot more than you need to do, in order to convert an image to greyscale.

There is a tutorial with code on how to do such conversion here: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]


这是一个不好的方法,但是很多原因.首先,这两个:1)太难,不可靠且难以维护,2)绝对不需要.

假设要使用System.Drawing,则需要执行以下操作:
  • 使用类System.Drawing.Bitmap;使用文件的构造函数之一从文件中读取位图.
  • 创建具有不同像素格式(无论您需要什么,例如每个像素8位)和相同大小的目标位图.
  • 使用System.Drawing.Bitmap.LockBits访问源位图和目标位图;
  • PROFIT!
This is a bad approach, but many reasons. First of all, the two: 1) it''s too difficult, unreliable and hard to maintain, 2) absolutely non needed.

Assuming you want to use System.Drawing, you need to do the following:
  • Use the class System.Drawing.Bitmap; read the bitmap from file using one of its constructors.
  • Create a target bitmap with the different pixel format (whatever you need, like 8 bits per pixel) and the same size.
  • Get access to source bitmap and target bitmaps using System.Drawing.Bitmap.LockBits; move data appropriately with the conversion of each bit you need.
  • PROFIT!


尝试
将C#RGB转换为基于调色板的8位灰度位图类 [
Try
C# RGB to Palette Based 8-bit Greyscale Bitmap Class[^]


这篇关于在C#中使用二进制读取器读取位图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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