简体   繁体   中英

How do I display the bitmap image in the picture box?

Store an image with the ".raw" extension in a two-dimensional byte array. Convert it to bitmap. I want to show this in the picture box, but if I run it with the code below, I get an error that the parameter is wrong.

Width and height are obtained from the information provided by the header file.

I wonder what I'm doing wrong.

string filename = @"test.raw";
byte[] rawBytes = File.ReadAllBytes(filename);
int bytePixel = 2;
int width = samples*bytePixel;
int height = lines;
byte[,] rawData = new byte[height, width];
int counter = new int();

for(int i = 0; i < height; i++)
{
    for(int j = 0; j < width; j++, counter++)
    {
        rawData[i, j] = rawBytes[counter];
    }
}
Bitmap bitmapImage = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
BitmapData bitmapImageData = bitmapImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormap.Format16bppGrayScale);

unsafe
{
    byte* pointer = (byte*)bitmapImageData.Scan0.ToPointer();
    for(int y = 0; y < height; y++)
    {
        for(int x = 0; x < width; x++, pointer++)
        {
            *pointer = rawData[y, x];
        }
    }
}
bitmapImage.UnlockBits(bitmapImageData);
pictureBox1.Image = bitmapImage;

Please give me some advice.

I can't figure out what's wrong but if you just want to see byte array result on screen, this func will make bmp file with IntPtr. Hope it helps.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var fs = new FileStream("test.bmp", FileMode.Open);

        Bitmap bitmap = new Bitmap(800, 600, PixelFormat.Format16bppGrayScale);

        BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, 800, 600), ImageLockMode.WriteOnly, PixelFormat.Format16bppGrayScale);

        unsafe
        {
            byte* p = (byte*)bitmapdata.Scan0.ToPointer();                
            for (int i = 0; i < 600; i++)
            {
                for (int j = 0; j < 800; j++)
                {
                    *p = (byte)(i * j); p++;
                }
            }
        }
        FileSaveBMP($"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.bmp", bitmapdata.Scan0, new CRect() { Width = 800, Height = 600 }, 800);
        bitmap.UnlockBits(bitmapdata);
        //pictureBox1.Image = bitmap;
    }

    private unsafe void FileSaveBMP(string sFile, IntPtr ptr, CRect rect, int w, int p_nByte = 1)
    {
        FileStream fs = new FileStream(sFile, FileMode.Create, FileAccess.Write);
        BinaryWriter bw = new BinaryWriter(fs);

        bw.Write(Convert.ToUInt16(0x4d42));
        if (p_nByte == 1)
        {
            if ((Int64)rect.Width * (Int64)rect.Height > Int32.MaxValue) bw.Write(Convert.ToUInt32(54 + 1024 + p_nByte * 1000 * 1000));
            else bw.Write(Convert.ToUInt32(54 + 1024 + p_nByte * (Int64)rect.Width * (Int64)rect.Height));
        }
        else if (p_nByte == 3)
        {
            if ((Int64)rect.Width * (Int64)rect.Height > Int32.MaxValue) bw.Write(Convert.ToUInt32(54 + p_nByte * 1000 * 1000));//uint bfSize = br.ReadUInt32();
            else bw.Write(Convert.ToUInt32(54 + p_nByte * (Int64)rect.Width * (Int64)rect.Height));//uint bfSize = br.ReadUInt32();
        }

        //image 크기 bw.Write();   bmfh.bfSize = sizeof(14byte) + nSizeHdr + rect.right * rect.bottom;
        bw.Write(Convert.ToUInt16(0));   //reserved // br.ReadUInt16();
        bw.Write(Convert.ToUInt16(0));   //reserved //br.ReadUInt16();
        if (p_nByte == 1)
            bw.Write(Convert.ToUInt32(1078));
        else if (p_nByte == 3)
            bw.Write(Convert.ToUInt32(54));//uint bfOffBits = br.ReadUInt32();

        bw.Write(Convert.ToUInt32(40));// uint biSize = br.ReadUInt32();
        bw.Write(Convert.ToInt32(rect.Width));// nWidth = br.ReadInt32();
        bw.Write(Convert.ToInt32(rect.Height));// nHeight = br.ReadInt32();
        bw.Write(Convert.ToUInt16(1));// a = br.ReadUInt16();
        bw.Write(Convert.ToUInt16(8 * p_nByte));     //byte       // nByte = br.ReadUInt16() / 8;                
        bw.Write(Convert.ToUInt32(0));      //compress //b = br.ReadUInt32();
        if ((Int64)rect.Width * (Int64)rect.Height > Int32.MaxValue) bw.Write(Convert.ToUInt32(1000 * 1000));// b = br.ReadUInt32();
        else bw.Write(Convert.ToUInt32((Int64)rect.Width * (Int64)rect.Height));// b = br.ReadUInt32();
        bw.Write(Convert.ToInt32(0));//a = br.ReadInt32();
        bw.Write(Convert.ToInt32(0));// a = br.ReadInt32();
        bw.Write(Convert.ToUInt32(256));      //color //b = br.ReadUInt32();
        bw.Write(Convert.ToUInt32(256));      //import // b = br.ReadUInt32();
        if (p_nByte == 1)
        {
            for (int i = 0; i < 256; i++)
            {
                bw.Write(Convert.ToByte(i));
                bw.Write(Convert.ToByte(i));
                bw.Write(Convert.ToByte(i));
                bw.Write(Convert.ToByte(255));
            }
        }
        if (rect.Width % 4 != 0)
        {
            rect.Right += 4 - rect.Width % 4;
        }
        byte[] aBuf = new byte[p_nByte * rect.Width];
        for (int i = rect.Height - 1; i >= 0; i--)
        {
            Marshal.Copy((IntPtr)((long)ptr + rect.Left + ((long)i + (long)rect.Top) * w * p_nByte), aBuf, 0, rect.Width * p_nByte);
            bw.Write(aBuf);
        }
        bw.Close();
        fs.Close();
    }
}


public class CRect
{
    public int Left
    {
        get; set;
    }
    public int Right
    {
        get; set;
    }
    public int Top
    {
        get; set;
    }
    public int Bottom
    {
        get; set;
    }
    public int Width
    {
        get; set;
    }
    public int Height
    {
        get; set;
    }
}

Above code creates image file like this. 图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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