gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:20:27 25 4
gpt4 key购买 nike

我根据我看到的一个例子做了这个,它从来没有抛出任何错误,但是图像显示为灰色。

有更好的方法吗?

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

关于c# - 如何在 C# 中将位图图像转换为 IntPtr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com