gdip image save directly intptr in my local drive

977 Views Asked by At

I have this code to get the image file from the scanner and save it on local disk:

                            IntPtr img = (IntPtr)pics[i];
                            SetStyle(ControlStyles.DoubleBuffer, false);
                            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                            SetStyle(ControlStyles.Opaque, true);
                            SetStyle(ControlStyles.ResizeRedraw, true);
                            SetStyle(ControlStyles.UserPaint, true);
                            bmprect = new Rectangle(0, 0, 0, 0);
                            bmpptr = GlobalLock(img);
                            pixptr = GetPixelInfo(bmpptr);
                            Gdip.SaveDIBAs(@"C:\", bmpptr, pixptr);

The problem is here Gdip.SaveDIBAs(@"C:\", bmpptr, pixptr);.the save dialog. enter image description here

I want to discard this dialog and save file directly in my drive.

**Updated:**



  public static bool SaveDIBAs(string picname, IntPtr bminfo, IntPtr pixdat)
        {
            SaveFileDialog sd = new SaveFileDialog();

            sd.FileName = picname;
            sd.Title = "Save bitmap as...";
            sd.Filter =
                "Bitmap file (*.bmp)|*.bmp|TIFF file (*.tif)|*.tif|JPEG file (*.jpg)|*.jpg|PNG file (*.png)|*.png|GIF file (*.gif)|*.gif|All files (*.*)|*.*";
            sd.FilterIndex = 1;

            return true;
        }
      for (int i = 0; i < pics.Count; i++)
                            {
                                IntPtr img = (IntPtr)pics[i];


                                SetStyle(ControlStyles.DoubleBuffer, false);
                                SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                                SetStyle(ControlStyles.Opaque, true);
                                SetStyle(ControlStyles.ResizeRedraw, true);
                                SetStyle(ControlStyles.UserPaint, true);

                                bmprect = new Rectangle(0, 0, 0, 0);

                                bmpptr = GlobalLock(img);
                                pixptr = GetPixelInfo(bmpptr);

                                SaveDIBAs(@"C:\a.jpg", bmpptr, pixptr);
    }
1

There are 1 best solutions below

7
György Kőszeg On

I think you should just simply use the built in Image and Bitmap types instead of directly calling the functions of the gdip.dll.

IntPtr img = (IntPtr)pics[i];
using (Bitmap bmp = Image.FromHBitmap(img))
{
     bmp.Save(@"C:\a.jpg", ImageFormat.Jpeg);
}