Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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();
    }
}
Posted
Updated 9-Oct-17 2:45am
v3
Comments
Richard MacCutchan 7-Oct-17 4:28am    
Please do not just dump a load of unformatted code and expect someone to debug it for you. Please edit your question, remove all the code not relevant to your question, add <pre> tags around the remainder, and explain exactly where and what is your problem.
Veerendra-13142768 9-Oct-17 2:55am    
thanks for your suggestion ,
you can see now edited the question
actually i know the 8bpp and 16bpp gray image to dislpay by taking a file which has bixel data in the raw format(the file name is veer.raw )
i want to display in the 24bpp so can you help me to display a raw imgage by reading a file from memory and display as image .i hope you will help me
Karthik_Mahalingam 11-Oct-17 23:53pm    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

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);


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900