本文介绍了如何直接定义8位灰度图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于创建8位位图

The following code is used to create a 8 bit bitmap

Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed);
BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat);

但是当我保存它时,它会另存为8位彩色位图.是否可以直接创建8位灰度位图而无需创建8位彩色位图,而以后又不得不将其转换为灰度级?

But when i save it it is saved as a 8 bit color bitmap. Is it possible to directly create a 8 bit grayscale bitmap without creating a 8 bit color bitmap and later having to convert it to a grayscale ?

推荐答案

如果您的位图具有调色板,那么实际上并没有bmp是否为灰度的概念.只要调色板中的所有颜色都是灰度的,那么bmp就是.

If you have a bitmap that has a palette then there's not really a notion of whether the bmp is greyscale or not. As long as all colours in the palette are greyscale then the bmp is.

此代码应该会有所帮助(这是VB,但应该易于翻译):

This code should help (it's VB but should be easy to translate):

Private Function GetGrayScalePalette() As ColorPalette
    Dim bmp As Bitmap = New Bitmap(1, 1, Imaging.PixelFormat.Format8bppIndexed)

    Dim monoPalette As ColorPalette = bmp.Palette

    Dim entries() As Color = monoPalette.Entries

    Dim i As Integer
    For i = 0 To 256 - 1 Step i + 1
        entries(i) = Color.FromArgb(i, i, i)
    Next

    Return monoPalette
End Function

在这里找到: http ://social.msdn.microsoft.com/Forums/zh-CN/vblanguage/thread/500f7827-06cf-4646-a4a1-e075c16bbb38

这篇关于如何直接定义8位灰度图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:12