How to convert a bitmap to IntPtr in C #?

I made it from the example I saw, it never threw any errors, but the image is displayed as gray.

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: I checked csproj to allow unsafe code in the assembly.

+3
source share
2 answers

Try it,

 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); } 
+5
source

If I understand correctly, you are trying to download a .bmp file. To do this, just use Image.FromFile () . Then you can do whatever you want.

+2
source

Source: https://habr.com/ru/post/970363/


All Articles