[英]How to convert a Bitmap Image to IntPtr in C#?
我是從看到的示例中得出的,它從未拋出任何錯誤,但是圖像顯示為灰色。
有一個更好的方法嗎?
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文件。 為此,只需使用Image.FromFile()即可 。 然后,您可以使用它做任何您想做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.