如何在C#中将位图图像转换为IntPtr?
我从我看到的一个例子中得出这个结论,它从未抛出任何错误,但图像显示为灰色 有更好的方法吗如何在C#中将位图图像转换为IntPtr?,c#,bitmap,intptr,C#,Bitmap,Intptr,我从我看到的一个例子中得出这个结论,它从未抛出任何错误,但图像显示为灰色 有更好的方法吗 private unsafe void menuItem7_Click(object sender, EventArgs e) { var settings = Utility.GatherLocalSettings(); openFileDialog1.InitialDirectory = settings.SavePath; openFileDi
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, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
try
{
pval=bd.Scan0;
...
}
finally
{
bmp.UnlockBits(bd);
}
如果我理解正确,您正在尝试加载.bmp文件。要做到这一点,只需使用。然后,你可以用它做任何你想做的事情。你可以发布frmContainer的代码,或者仅仅是它的构造函数,它带有一个IntPtr参数吗?别忘了在之后解锁位!bmp.UnlockBits(bd);同意!谢谢你的评论。作为进一步的参考-在我更改像素格式之前,我的程序一直在崩溃:BitmapData bd=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,bmp.PixelFormat);