如何将位图图像转换为IntPtr的在C#中? [英] How to convert a Bitmap Image to IntPtr in C#?

查看:430
本文介绍了如何将位图图像转换为IntPtr的在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个例子,我看到这个做,它从来不浪费任何错误,但图像显示为灰色。

I made this from an example i saw, it never threw any error, but the image is displayed as grey.

有没有更好的方法来做到这一点?

Is there a better way to do this?

private unsafe void menuItem7_Click(object sender, EventArgs e)
    {
        var settings = Utility.GatherLocalSettings();

        openFileDialog1.InitialDirectory = settings.SavePath;
        openFileDialog1.Filter = "Scan Files (*.jpg)|*.jpg";
        openFileDialog1.FilterIndex = 1;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            byte[] openFile = File.ReadAllBytes(openFileDialog1.FileName);
            fixed (byte* p = openFile)
            {
                IntPtr img = (IntPtr)p;

                frmContainer newScan = new frmContainer(img);
                newScan.MdiParent = this;
                newScan.Text = Path.GetFileName(openFileDialog1.FileName) + " [Saved]";
                newScan.Show();
            }
        }

    }

PS:我检查的csproj允许在构建不安全code

PS: I checked the csproj to allow unsafe code in the build.

推荐答案

试试这个,

IntPtr pval = IntPtr.Zero;
System.Drawing.Imaging.BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
try
{
    pval=bd.Scan0;

    ...
}
finally
{
    bmp.UnlockBits(bd);
}

这篇关于如何将位图图像转换为IntPtr的在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆