在.NET Format16bppGrayScale图像设置单个像素 [英] Set individual pixels in .NET Format16bppGrayScale image

查看:1279
本文介绍了在.NET Format16bppGrayScale图像设置单个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图呈现一个小位在内存中的.NET,需要为16位灰度。位图的格式设置为PixelFormat.Format16bppGrayScale。然而,Bitmap.SetPixel需要一个颜色参数。颜色反过来需要一个字节的每个R,B和G(和任选的A)。

I'm trying to render a small Bitmap in memory with .NET that needs to be 16 bit Grayscale. The bitmap's format is set to PixelFormat.Format16bppGrayScale. However, Bitmap.SetPixel takes a Color argument. Color in turn takes one byte for each of R, B, and G (and optionally A).

如何指定一个16位的灰度值,而不是一个8位的值绘制到我的位图的时候?

How do I specify a 16-bit gray scale value rather than an 8 bit value when drawing to my bitmap?

推荐答案

无论图像格式,与setPixel()是残酷缓慢。我从来没有用它在实践中。

Regardless of the image format, SetPixel() is brutally slow. I never use it in practice.

您可以使用LockBits方法,它可以让您快速编组管理数据到非托管位字节快得多设置像素。

You can set pixels much faster using the LockBits method, which allows you to quickly marshal managed data to the unmanaged bitmap bytes.

下面是如何可能看一个例子:

Here is an example of how that might look:

Bitmap bitmap = // ...

// Lock the unmanaged bits for efficient writing.
var data = bitmap.LockBits(
    new Rectangle(0, 0, bitmap.Width, bitmap.Height),
    ImageLockMode.ReadWrite,
    bitmap.PixelFormat);

// Bulk copy pixel data from a byte array:
Marshal.Copy(byteArray, 0, data.Scan0, byteArray.Length);

// Or, for one pixel at a time:
Marshal.WriteInt16(data.Scan0, offsetInBytes, shortValue);

// When finished, unlock the unmanaged bits
bitmap.UnlockBits(data);

需要注意的是16bpp的灰度显示通过GDI +是不支持的,这意味着.NET不帮忙拯救一个16bpp的灰度位图文件或流。请参阅<一href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab">http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab

这篇关于在.NET Format16bppGrayScale图像设置单个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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