gdip image save directly intptr in my local drive
Asked Answered
D

1

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);
    }
Depredate answered 13/9, 2016 at 8:16 Comment(4)
It should be OK once you change your line Gdip.SaveDIBAs(@"C:\", bmpptr, pixptr); to Gdip.SaveDIBAs(@"C:\a.jpg", bmpptr, pixptr);Cropland
@KeyurPATEL i changed to .jpg but it didn't workDepredate
Check this out: C# 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);Cropland
@KeyurPATEL so yes this is my problem in my question i want to find that wayDepredate
S
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);
}
Skirling answered 13/9, 2016 at 8:31 Comment(7)
And if you add using System.Drawing;?Selfstarter
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.Selfstarter
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 underminedDepredate
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.Selfstarter
i don't have any assembly or system dll named System.Drawing.Image.FromHBitmapDepredate
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;Selfstarter
Thank you but i get this error :A generic error occurred in GDI+.Depredate

© 2022 - 2024 — McMap. All rights reserved.