如何使用C#显示24bpp原始图像 [英] How to display 24bpp raw images using C#
本文介绍了如何使用C#显示24bpp原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我使用原始图像显示器有16位和8位选项显示原始图像为灰色8/16但我想在24bpp显示图像rgb /bgr.code我使用附在下面。可以任何人帮我解决这个问题
我尝试过:
Hello guys ,
I am using raw image Displayer which has 16bit and 8bit option to display raw images as gray 8/16 but i wanted to display image in 24bpp rgb /bgr.code which i use is attached below .can any one help me to sort out of yhis problem
What I have tried:
private void DisplayImage16(string fileName)
{
// Open a binary reader to read in the pixel data.
// We cannot use the usual image loading mechanisms since this is raw
// image data.
try
{
BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
ushort pixShort;
int i;
long iTotalSize = br.BaseStream.Length;
int iNumberOfPixels = (int)(iTotalSize /2);
// Get the dimensions of the image from the user
ID = new ImageDimensions(iNumberOfPixels);
if (ID.ShowDialog() == true)
{
width = Convert.ToInt32(ID.tbWidth.Text);
height = Convert.ToInt32(ID.tbHeight.Text);
canvas.Width = width;
canvas.Height = height;
img.Width = width;
img.Height = height;
pix16 = new ushort[iNumberOfPixels];
for (i = 0; i < iNumberOfPixels; ++i)
{
pixShort = (ushort)(br.ReadUInt16());
//pix16[i] = pixShort;
}
br.Close();
int bitsPerPixel = 24;
stride = (width * bitsPerPixel *3);
// Single step creation of the image
bmps = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray16, null,
pix16, stride);
img.Source = bmps;
bnSaveJPG.IsEnabled = true;
bnSavePNG.IsEnabled = true;
}
// else
{
br.Close();
}
}
catch (Exception e)
{
// continue;
// MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void DisplayImage08(string fileName)
{
// Open a binary reader to read in the pixel data.
// We cannot use the usual image loading mechanisms since this is raw
// image data.
try
{
BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
byte pixByte;
int i;
int iTotalSize = (int)br.BaseStream.Length;
// Get the dimensions of the image from the user
ID = new ImageDimensions(iTotalSize);
if (ID.ShowDialog() == true)
{
width = Convert.ToInt32(ID.tbWidth.Text);
height = Convert.ToInt32(ID.tbHeight.Text);
canvas.Width = width;
canvas.Height = height;
img.Width = width;
img.Height = height;
pix08 = new byte[iTotalSize];
for (i = 0; i < iTotalSize; ++i)
{
pixByte = (byte)(br.ReadByte());
pix08[i] = pixByte;
}
br.Close();
int bitsPerPixel = 24;
stride = (width * bitsPerPixel +7)/24;
// Single step creation of the image
bmps = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null,
pix08, stride);
img.Source = bmps;
bnSaveJPG.IsEnabled = true;
bnSavePNG.IsEnabled = true;
}
// else
{
//br.Close();
}
}
catch (Exception e)
{
//MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void bnSavePNG_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PNG Images (.png)|*.png";
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
string targetPath = "";
// Process save file dialog box results
if (result == true)
{
// Save document
targetPath = dlg.FileName;
FileStream fs = new FileStream(targetPath, FileMode.Create);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmps));
encoder.Save(fs);
fs.Close();
}
}
推荐答案
您可以像这样填写BitmapSource的颜色数据:
You can fill in the colour data for a BitmapSource like this:
PixelFormat pf = PixelFormats.Bgr24;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];
int rgbIndex = 0;
for (int lineIndex = 0; lineIndex < height; lineIndex++)
{
int lineStart = lineIndex * rawStride;
for (int pixelIndex = 0; pixelIndex < width; pixelIndex++)
{
rawImage[lineStart++] = (byte)(br.ReadByte()); // Red
rawImage[lineStart++] = (byte)(br.ReadByte()); // Green
rawImage[lineStart++] = (byte)(br.ReadByte()); // Blue
}
}
image = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride);
不知道原始数据的格式我不能告诉你正确的读取文件的方法。上面的代码假设数据存储在RGBRGB..etc中,没有任何间隙。
Not knowing what format the raw data is in I can not advise you on the correct way to read the file. The code above assumes the data is stored RGBRGB..etc with no gaps anywhere.
这篇关于如何使用C#显示24bpp原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文