【问题标题】:How do I display the bitmap image in the picture box?如何在图片框中显示位图图像?
【发布时间】:2022-11-16 15:17:31
【问题描述】:

将带有“.raw”扩展名的图像存储在二维字节数组中。将其转换为位图。我想在图片框中显示这个,但是如果我用下面的代码运行它,我会得到一个参数错误的错误。

宽度和高度是从头文件提供的信息中获得的。

我想知道我做错了什么。

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;

请给我一些建议。

【问题讨论】:

    标签: c# winforms image-processing


    【解决方案1】:

    我不知道出了什么问题,但如果你只想在屏幕上看到字节数组结果,这个函数将使用 IntPtr 生成 bmp 文件。希望能帮助到你。

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

    上面的代码像这样创建图像文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2014-12-17
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多