繁体   English   中英

如何直接定义8位灰度图像?

[英]How can i define a 8 bit grayscale image directly ?

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

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位彩色位图,以后必须将其转换为灰度?

我刚刚遇到这个问题并在此帮助下解决了它: 在这里输入链接描述

但我会在这里提出快速解决方案,希望对您有所帮助。 :) BITMAP文件包含四个部分,一个是调色板,它决定了颜色的显示方式。 所以我们在这里修改Bitmap文件的调色板:

    myBMP = new Bitmap(_xSize, _ySize, _xSize, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, _pImage);
    //_pImage is the pointer for our image
    //Change palatte of 8bit indexed BITMAP. Make r(i)=g(i)=b(i)
    ColorPalette _palette = myBMP.Palette;
    Color[] _entries = _palette.Entries;
    for (int i = 0; i < 256; i++)
    {
        Color b = new Color();
        b = Color.FromArgb((byte)i, (byte)i, (byte)i);
        _entries[i] = b;
    }
    myBMP.Palette = _palette;
    return myBMP;

如果你有一个具有调色板的位图,那么bmp是否是灰度的概念并不存在。 只要调色板中的所有颜色都是灰度,那么bmp就是。

这段代码应该有用(它是VB,但应该很容易翻译):

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/en-us/vblanguage/thread/500f7827-06cf-4646-a4a1-e075c16bbb38

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM