加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C#中将位图图像转换为IntPtr?

发布时间:2020-12-16 01:31:51 所属栏目:百科 来源:网络整理
导读:我从我看到的一个例子中做到了这一点,它从未抛出任何错误,但图像显示为灰色. 有一个更好的方法吗? private unsafe void menuItem7_Click(object sender,EventArgs e) { var settings = Utility.GatherLocalSettings(); openFileDialog1.InitialDirectory =
我从我看到的一个例子中做到了这一点,它从未抛出任何错误,但图像显示为灰色.

有一个更好的方法吗?

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以允许构建中的不安全代码.

解决方法

试试这个,

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读