7

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);
    }
Ehsan Akbar
  • 5,728
  • 15
  • 72
  • 137
  • It should be OK once you change your line `Gdip.SaveDIBAs(@"C:\", bmpptr, pixptr);` to `Gdip.SaveDIBAs(@"C:\a.jpg", bmpptr, pixptr);` – Keyur PATEL Sep 13 '16 at 08:35
  • @KeyurPATEL i changed to .jpg but it didn't work – Ehsan Akbar Sep 13 '16 at 08:36
  • Check this out: [C# Save image without using Dialog](http://stackoverflow.com/questions/15886617/c-sharp-save-image-without-using-dialog). It is not completely relevant, but it shows that saving without dialog can be done, if you specify everything correctly. Try using `FileInfo fi = new FileInfo(filePath);` as the answer in the link suggests, then `Gdip.SaveDIBAs(@"C:\" + fi.Name, bmpptr, pixptr);` – Keyur PATEL Sep 13 '16 at 08:39
  • @KeyurPATEL so yes this is my problem in my question i want to find that way – Ehsan Akbar Sep 13 '16 at 08:52

1 Answers1

0

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);
}
György Kőszeg
  • 13,565
  • 3
  • 27
  • 53
  • And if you add `using System.Drawing;`? – György Kőszeg Sep 13 '16 at 08:36
  • If you added the reference and it still cannot be resolved, then it might be a namespace or type conflict (do you have another `Image` type in your project?). Try to add the full namespace prefix in my code: `System.Drawing.Image.FromHBitmap`. – György Kőszeg Sep 13 '16 at 08:42
  • i try this one and it saved the image , Bitmap newBitmap = new Bitmap(2450, 2450, 2452, PixelFormat.Format32bppRgb, img); newBitmap.Save(@"c:\"); .but when i open the image the image is undermined – Ehsan Akbar Sep 13 '16 at 08:43
  • That `Bitmap` constructor awaits another pointer, which is the Scan0 inside of a `BitmapData`, If your pointer is a GDI+ bitmap, you should use the `System.Drawing.Image.FromHBitmap` method. – György Kőszeg Sep 13 '16 at 08:49
  • i don't have any assembly or system dll named System.Drawing.Image.FromHBitmap – Ehsan Akbar Sep 13 '16 at 08:50
  • The using clause should contain only `using System.Drawing;`. Now, `Bitmap` and `Image` types should be visible. But since you have problems with `Image`, I suspect you have a custom `Image` type somewhere in your project, which causes a conflict. To resolve this, you can either fully specify the `Image` type in my code: `bmp = System.Drawing.Image.FromHBitmap(img)`, OR, you should add an extra using clause to define an alias for the `Image` type: `using Image = System.Drawing.Image;` – György Kőszeg Sep 13 '16 at 09:01
  • Thank you but i get this error :A generic error occurred in GDI+. – Ehsan Akbar Sep 13 '16 at 09:05