GDI+ Create a thumbnail image and save it to disk.

Started by Robert Eaton, December 28, 2011, 08:43:29 PM

Previous topic - Next topic

Robert Eaton

Hi,
I'm looking for a way to use GDI+ to read a jpeg image and save out a small thumbnail image.
I've used Freeimage in the past (thanks to Jose) and maybe that's a better choice but it seems GDI should work for this simple task. I've shown some junk code that I've pieced together. It will load an image into a picture control and scale it to 100 x 100 pixels in the control (just as a quick example). The code will then save the image back out in Bmp format, but it saves it at the original dimensions. Maybe I have to capture the image but I imagine that this could all be done in memory. This is where I am at the moment.

Can anyone point me in the right direction?
Thanks,
Bob

Function Thunbnail_test ()As Long

Local hdc As Dword
Local wszLoadFileName As wStringZ * 255
Local wszSaveFileName As wStringZ * 255
Local hr As Long
Local wszMimeType As wStringZ * 255
Local s As String
Local clsidEncoder As Guid

wszLoadFileName = "C:\input.jpg" 'change as needed
wszSaveFileName = "C:\out.bmp" 'change as needed

wszMimeType = "image/bmp"

   If Trim$(wszLoadFileName) = "" Then Function = %StatusInvalidParameter : Exit Function
   If Trim$(wszSaveFileName) = "" Then Function = %StatusInvalidParameter : Exit Function

   s = GdiPlusGetEncoderClsid(wszMimeType)
   If s = "" Then
         Function = %StatusInvalidParameter
      Exit Function
   End If
   clsidEncoder = Guid$(s)

   hdc = GetDC(HWND_FRMMAIN_PICTURE1)

   Local graphics As Dword
   GdipCreateFromHDC(hdc, graphics)
   
   Local pBitmap As Dword
   GdipCreateBitmapFromFile(wszLoadFileName, pBitmap)

'Draw the Bitmap       
   hr = GdipDrawImageRectI(Graphics, pBitmap, 0, 0, 200, 100)
   
   'saves the image in bmp format but at same dimensions as original
   hr = GdipSaveImageToFile(pBitmap, wszSaveFileName, clsidEncoder, ByVal %Null)
   
   ' // Cleanup
   GdipDisposeImage(pBitmap)
   GdipDeleteGraphics(graphics)

End Function


Robert Eaton

Hi Jose,
That did the trick!
I saw that on your site but didn't connect the dots.

Thanks,
Bob