Author
|
Topic: EZ_GDIP_LoadPicture
|
Dan Gin zel Member
|
posted 04-29-2009 06:51 PM
Here's the first routine that I've been working with for the past couple of days. By using GDI+, one is able to load image files other than BMP, such as JPG, PNG and TIFF. I've taken this a step further by adding an integral aspect-ratio-correct resize option using the smooth resizing features of GDI+. Here's an example of the difference: Let me know what y'all think or if y'all find any issues. code:
' These include files courtesy of José Roca at http://www.jose.it-berater.org #Include "tb_gdiplus.inc" #Include "tb_iunknown.inc" ' Although not a 1:1 replacement of EZ_LoadPicture, EZ_GDIP_LoadPicture() will load from BMP, PNG, JPG and other file types ' as supported by GDI+. Basic usage: ' P$ = EZ_GDIP_LoadPicture (FileName$) ' Furthermore, additional optional parameters can specify maximum dimensions and will cause the image to be sized automatically, ' if necessary. All resizing maintains the aspect ratio of the original image. ' P$ = EZ_GDIP_LoadPicture (FormName$, MaxWidth&, MaxHeight&, ForceToFit&) ' MaxWidth& and MaxHeight& are the maximum dimensions (in pixels.) ' If ForceToFit = 0, only images larger than MaxWidth& and/or MaxHeight& will be reduced to fit. ' If ForceToFit = 1, all images will be enlarged or reduced to fit the dimensions. ' If ForceToFit = -1, no resizing will occur. Declare Function EZ_GDIP_LoadPicture (ImageSourceFile as String, Opt MaxW as Long, Opt MaxH as Long, Opt ForceToFit as Long) as String Function EZ_GDIP_LoadPicture (ImageSourceFile as String, Opt MaxW as Long, Opt MaxH as Long, Opt ForceToFit as Long) as String Local StartupInput as GdiplusStartupInput Local GDIToken as Long Local pImage as DWord Local Picture as String Local Success, Result as Long Local sW, sH as Single Local iW, iH as Long Local w, h as Long Local hNewLogo as Long Local pGraphics as Long
StartupInput.GDIPlusVersion = 1 IF GDIplusStartup(GDItoken, StartupInput, BYVAL %NULL) = 0 THEN Success = 1 Result = GDIPLoadImageFromFile (UCode$(ImageSourceFile), pImage) If Result <> 0 Then Success = 0 : Exit If GDIPGetImageDimension pImage, sW, sH iW = sW ' Image Width from the Single iH = sH If MaxW > 0 and MaxH > 0 Then ' The user specified the maximum size, so let's see if we need to resize Select Case ForceToFit Case 1 ' Yes, Always Enlarge or Reduce to Fit w = iW / Max(iW / MaxW, iH / MaxH) h = iH / Max(iW / MaxW, iH / MaxH) Case 0 ' No, Only Reduce to Fit If iw > MaxW or ih > MaxH Then w = iW / Max(iW / MaxW, iH / MaxH) h = iH / Max(iW / MaxW, iH / MaxH) Else w = iW h = iH End If Case Else w = iW h = iH End Select Else w = iw h = iH End If Picture = EZ_CreatePicture(w, h) hNewLogo = EZ_StartPictureDraw(Picture) Result = GDIPCreateFromHDC(hNewLogo, pGraphics) If Result <> 0 Then Success = 0 : Exit If Result = GDIPSetInterpolationMode(pGraphics, %QualityModeHigh) If Result <> 0 Then Success = 0 : Exit If Result = GDIPDrawImageRectI(pGraphics, pImage, 0, 0, w, h) If Result <> 0 Then Success = 0 : Exit If EZ_EndPictureDraw Function = Picture IF pImage Then GDIPDisposeImage(pImage) GDIplusShutdown GDItoken End If If Success = 0 Then Function = EZ_LoadPicture(ImageSourceFile) End If End Function
|
Dan Gin zel Member
|
posted 04-29-2009 06:58 PM
I figured I'd post the Sub that I used to display the image with the little drop shadow and all.code:
Sub DrawLogo (FormName$, CID&, CurrentLogoFile as String) Local pw, ph as Long ' Picture Width, Height Local cw, ch as Long ' Canvas Width, Height Local w, h as Long ' Resulting Width, Height Local pImage as String ' The Picture EZ_GetCanvasSize FormName$, CID&, cw, ch EZ_StartDraw FormName$, CID&, cw, ch, "" EZ_Color 460, 460 ' this is my dialog background color EZ_CDraw %EZ_RECT, 0, 0, cw, ch, 0, 1 ' GDI+ version pImage = EZ_GDIP_LoadPicture (CurrentLogoFile, cw - 5, ch - 5) EZ_GetPictureSize pImage, w, h ' EZGUI/GDI version ' pImage = EZ_LoadPicture (CurrentLogoFile) ' EZ_GetPictureSize pImage, pw, ph ' w = pw / Max(pw / (cw - 5), ph / (ch - 5)) ' h = ph / Max(pw / (cw - 5), ph / (ch - 5)) EZ_Color 8, 8 ' this is the gray color for the drop shadow EZ_CDraw %EZ_Rect, 5, 5, w + 5, h + 5, 0, 1 EZ_CDrawPicture 0, 0, w, h, pImage, "" EZ_Color 0, 0 ' this is for the thin black frame around the image EZ_CDraw %EZ_Rect, 0, 0, w, h, 1, 0 EZ_EndDraw EZ_FreeImage pImage End Sub
You can see that I commented out the process using straight EZ_LoadPicture that then calculates resulting size to display. I did this so that I could jump back and forth during my testing phase. [This message has been edited by Dan Gin zel (edited 05-04-2011).] | |