C# 将灰度8(8位)图像转换为rgb888(24位)图像
我的问题是,当拍摄一张8位灰度图像并将其转换为24位rgb24bpp时,我的图像会放大,颜色比例会混淆(请参阅)。 我的计划是通过客户机-服务器实现在程序中显示GigE Cam。因此,我需要它的图片,以获得灰度,因为日期率的问题,但我只得到这个错误的大小和图片。只有将它们转换为myImage/myImageFormat时才会发生这种情况。我的问题是,我需要这张照片,以便以后进行测量 我用于转换图像的代码如下所示:C# 将灰度8(8位)图像转换为rgb888(24位)图像,c#,image,.net-4.5,C#,Image,.net 4.5,我的问题是,当拍摄一张8位灰度图像并将其转换为24位rgb24bpp时,我的图像会放大,颜色比例会混淆(请参阅)。 我的计划是通过客户机-服务器实现在程序中显示GigE Cam。因此,我需要它的图片,以获得灰度,因为日期率的问题,但我只得到这个错误的大小和图片。只有将它们转换为myImage/myImageFormat时才会发生这种情况。我的问题是,我需要这张照片,以便以后进行测量 我用于转换图像的代码如下所示: public myImage(Bitmap bmp, myImageFormat
public myImage(Bitmap bmp, myImageFormat format)
{
if (bmp == null)
{
throw new ArgumentNullException("bmp");
}
this.Height = bmp.Height;
this.Width = bmp.Width;
this.Format = format;
// copy bmp matrix without padding to rgb or grey array
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
//if (format == myImageFormat.Grey8)
//{ return; }
//else
//{
int factor = 3;
if (format == myImageFormat.Grey8)
{
factor = 1;
// save grey scale palette
m_colorPaletteColorData = new Color[bmp.Palette.Entries.Length];
for (int i = 0; i < bmp.Palette.Entries.Length; i++)
{
m_colorPaletteColorData[i] = bmp.Palette.Entries[i];
}
//return;
}
else if (format == myImageFormat.Rgb888
&& bmp.PixelFormat == PixelFormat.Format8bppIndexed)
{
// convert all type of bitmaps to 24-Bit bitmaps by using GDI+
Bitmap bmp24 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb);
bmp24.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution); // prevent resolution conversion
using (Graphics g = Graphics.FromImage(bmp24))
{
g.PageUnit = GraphicsUnit.Pixel; // prevent DPI conversion
g.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height);
}
bmp = bmp24;
}
BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); // LOCKED
int bytes = bmp.Height * bmp.Width * factor;
int byteIndex = 0;
m_imageData = new byte[bytes];
unsafe
{
for (int y = 0; y < bmpData.Height; y++)
{
byte* row = (byte*)bmpData.Scan0 + (y * bmpData.Stride);
for (int x = 0; x < bmpData.Width; x++)
{
m_imageData[byteIndex] = row[x * factor];
byteIndex++;
if (format == myImageFormat.Rgb888)
{
m_imageData[byteIndex] = row[x * factor + 1];
byteIndex++;
m_imageData[byteIndex] = row[x * factor + 2];
byteIndex++;
}
}
}
}
bmp.UnlockBits(bmpData); // UNLOCKED
//}
}
/// <summary>
/// returns a 8bppGrey or 24bppRgb Bitmap
/// </summary>
public Bitmap ToBitmap()
{
if (m_imageData == null)
{
throw new InvalidOperationException("Internal image data does not exist!");
}
PixelFormat pixelFormat;
int factor = 3;
if (this.Format == myImageFormat.Grey8)
{
pixelFormat = PixelFormat.Format8bppIndexed;
factor = 1;
}
else
{
pixelFormat = PixelFormat.Format24bppRgb;
}
m_bmp = new Bitmap(this.Width, this.Height, pixelFormat);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
BitmapData bmpData = m_bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, pixelFormat); // LOCKED
int counter = 0;
unsafe
{
for (int y = 0; y < bmpData.Height; y++)
{
byte* row = (byte*)bmpData.Scan0 + (y * bmpData.Stride);
for (int x = 0; x < bmpData.Width; x++)
{
byte color = m_imageData[counter];
row[x * factor] = color;
counter++;
if (this.Format == myImageFormat.Rgb888)
{
row[x * factor + 1] = m_imageData[counter];
counter++;
row[x * factor + 2] = m_imageData[counter];
counter++;
}
}
}
}
// copy original grayscale color palette, otherwise it will be a 8bit RGB bitmap
if (this.Format == myImageFormat.Grey8)
{
ColorPalette palette = m_bmp.Palette;
Color[] entries = palette.Entries;
if (m_colorPaletteColorData.Length >= entries.Length)
{
for (int i = 0; i < entries.Length; i++)
{
entries[i] = m_colorPaletteColorData[i];
}
}
m_bmp.Palette = palette;
}
m_bmp.UnlockBits(bmpData); // UNLOCKED
return m_bmp;
}
公共myImage(位图bmp、myImageFormat)
{
如果(bmp==null)
{
抛出新的ArgumentNullException(“bmp”);
}
this.Height=bmp.Height;
this.Width=bmp.Width;
this.Format=Format;
//将bmp矩阵无填充复制到rgb或灰色阵列
矩形rect=新矩形(0,0,bmp.Width,bmp.Height);
//if(format==myImageFormat.Grey8)
//{return;}
//否则
//{
整数因子=3;
if(format==myImageFormat.Grey8)
{
系数=1;
//保存灰度调色板
m_colorPaletteColorData=新颜色[bmp.palete.Entries.Length];
对于(int i=0;i如果传入一个24位位图以转换为8位灰色,则无法复制有效的源调色板,因为24位格式没有索引。因此,当您将调色板从24位源图像(在构造函数中)保存到
m_colorplatettecolordata[]
中,然后将其设置在8位灰度图像上(在ToBitmap()中),该调色板实际上包含什么?在押