920517 - 如何获取图像的像素? [英] 920517 - how to get pixels of an Image?

查看:125
本文介绍了920517 - 如何获取图像的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Image对象。它是单色的。我需要获取图像的像素并将每个相邻的8个像素保存为一个字节。

如何在C#中执行此操作?

例如,如果顶部的八个像素图像的左边是白色,白色,黑色,黑色,黑色,白色,白色和黑色我想在输出文件中写入值0x39。

i have an Image object. it's monochrome. i need to get pixels of the image and save each adjacent 8 pixels as a byte.
how can i do it in C#?
for example if the eight pixels from top left of image are white, white, black, black, black, white, white and black i want to write value 0x39 in the output file.

推荐答案

用于检查以下提到的链接。



使用LockBits方法访问图像数据



http://bobpowell.net/lockingbits.aspx [ ^ ]



欲了解更多信息:



http://stackoverflow.com/ question / 190385 / how-to-manipulate-pixel-at-pixel-level-in-c [ ^ ]



我希望这会对你有所帮助。
For that check below mentioned links.

Using the LockBits method to access image data

http://bobpowell.net/lockingbits.aspx[^]

For more info :

http://stackoverflow.com/questions/190385/how-to-manipulate-images-at-pixel-level-in-c[^]

I hope this will help to you.


这样的事可能会为你解决。



Something like this might solve it for you.

using System.Drawing;
using System.Text;
using System;

namespace ParseTest
{
    internal class Program
    {
        private const int Black = unchecked((int)0xFF000000);
        private const int White = unchecked((int)0xFFFFFFFF);

        private static bool IsValid(Color pixel)
        {
            var argb = pixel.ToArgb();
            return argb == Black || argb == White;
        }

        private static int GetBlackBit(Color pixel) {
            var argb = pixel.ToArgb();
            return argb == Black ? 1 : 0;
        }

        static void Main()
        {
            var image = (Bitmap)Image.FromFile(@"C:\Temp\Input.png");
            var result = new StringBuilder();

            for (var y = 0; y < image.Size.Height; ++y)
            {

                for (var x = 0; x < image.Size.Width; x += 8)
                {
                    var value = 0;
                    for (var i = 0; i < 8; ++i)
                    {
                        var pixel = image.GetPixel(x + i, y);
                        if (!IsValid(pixel))
                            throw new Exception("Image is not monochrome!!!");

                        value |= GetBlackBit(pixel) << (7 - i);
                    }
                    result.AppendFormat("0x{0:X2} ", value);
                }
                result.AppendLine();
            }
            Console.WriteLine(result);
        }
    }
}



(请注意,该示例仅适用于宽度为8的倍数的图像)。



请注意,通过锁定位并直接获取数据可能会提高性能,但使用指针意味着您需要将代码标记为不安全



希望这会有所帮助,

Fredrik


(note that the example only works on images with a width that is a multiple of 8).

Note that you might get a performance boost from locking the bits and getting the data directly, but using pointers means you need to mark your code as unsafe.

Hope this helps,
Fredrik


这篇关于920517 - 如何获取图像的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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