Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Winforms应用程序中显示二维整数数组中的图像_C#_Winforms_Imaging - Fatal编程技术网

C# 如何在Winforms应用程序中显示二维整数数组中的图像

C# 如何在Winforms应用程序中显示二维整数数组中的图像,c#,winforms,imaging,C#,Winforms,Imaging,我需要在Winforms应用程序中将2D数组(UInt16[,])显示为16位灰度图像 我需要采取的步骤大致如下: 从UInt16[,]数组构建位图——请注意,我需要高效地执行此操作,因为我希望最终以60 Hz或更高的频率更新图片 在主窗口上绘制位图,可能在图片盒中,以60 Hz或更高频率更新 我想根据PictureBox ,我得到了高效(但不安全)的代码,可以从数组中构建位图 此外,我还检查了一下,以确保我至少可以画出一个事先制作好的图像而不会出现问题,而且效果很好 但是,当我试图显示由UIn

我需要在Winforms应用程序中将2D数组(
UInt16[,]
)显示为16位灰度图像

我需要采取的步骤大致如下:

  • UInt16[,]
    数组构建
    位图
    ——请注意,我需要高效地执行此操作,因为我希望最终以60 Hz或更高的频率更新图片

  • 在主窗口上绘制
    位图
    ,可能在
    图片盒中,以60 Hz或更高频率更新

  • 我想根据
    PictureBox

  • ,我得到了高效(但不安全)的代码,可以从数组中构建
    位图

    此外,我还检查了一下,以确保我至少可以画出一个事先制作好的图像而不会出现问题,而且效果很好

    但是,当我试图显示由
    UInt16[,]
    生成的
    位图时,我遇到了或(在另一台机器上运行)遇到了
    系统。ArgumentException
    ,“参数无效。”

    在遇到故障后,我获取了代码以确定位图是否为空。这不是问题所在

    下面是一些使用虚拟数组的示例代码

    主表单代码

    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    
    namespace IntArrViewer
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                int sz = 256;
                UInt16[,] raw = new UInt16[sz, sz];
    
                // Assign values to pixels
                for (int i = 0; i < sz; i++)
                    for (int j = 0; j < sz; j++)
                        raw[i, j] = (UInt16)(i * sz + j);
    
                // Get Bitmap from 2D array of UInt16
                Bitmap intBmp= UInt16toBmp(raw, sz, sz);
                // Get Bitmap from premade JPG
                Bitmap preMadeBmp= new Bitmap(@"premade_pic.jpg");
                // Verify that neither BMP is empty
                bool emptyIntBmp= BmpIsEmpty(intBmp);
                bool emptyPreMadeBmp= BmpIsEmpty(preMadeBmp);
                // Set main picture box to premade image
                mainPic.Image = preMadeBmp;
                // Setting to array image raises an exception
                // mainPic.Image = intBmp
            }
    
    
            public unsafe Bitmap UInt16toBmp(UInt16[,] frame, int width, int height)
            {
                int stride = width * 2;
                Bitmap bitmap;
                fixed (UInt16* intPtr = &frame[0, 0])
                {
                    bitmap = new Bitmap(width, height, stride,
                                        PixelFormat.Format16bppGrayScale,
                                        new IntPtr(intPtr));
                }
                return bitmap;
            }
    
            public bool BmpIsEmpty(Bitmap bmp)
            {
                var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, 
                                                      bmp.Height), 
                                        ImageLockMode.ReadOnly, 
                                        bmp.PixelFormat);
                var bytes = new byte[data.Height * data.Stride];
                Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
                bmp.UnlockBits(data);
                return bytes.All(x => x == 0);
             }
        }
    }
    
    使用System.Drawing.Imaging;
    使用System.Runtime.InteropServices;
    命名空间IntArrViewer
    {
    公共部分类Form1:Form
    {
    公共表格1()
    {
    初始化组件();
    int sz=256;
    UInt16[,]原始=新UInt16[sz,sz];
    //为像素赋值
    对于(int i=0;ix==0);
    }
    }
    }
    
    不幸的是,16bppgrayscale已定义,但实际上并未定义。如果运气好的话,您可能会成功地创建有效的图像,这些图像可以在其他查看器中显示,但不能在PictureBox或其他.Net/Winforms控件中显示。哦,该死的,这解释了我的沮丧!如果有某种形式的警告标志或抛出的东西就好了。如果我能把这两个小时弄回来,那也太好了:-)所以看起来前进的方向是做一个ARGB位图,但只使用灰度?非常感谢你的评论,我希望我能给你打分是的。也可以创建8位灰度图像。有关创建其选项板的示例,请参见和。。