Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GDI+ how to convert graphics to bitmap? (C++ or ahk)


  • Please log in to reply
1 reply to this topic
BrandonHotkey
  • Members
  • 691 posts
  • Last active: Oct 21 2015 09:41 PM
  • Joined: 21 May 2012
The GdipCreateBitmapFromGraphics creates a new, blank, usually solid black bitmap in memory which can be used with GDI+, not a clipped copy of the image.

 

I have this code:

GpGraphics * pG;
// I create graphics from bitmap
result = Gdiplus::DllExports::GdipGetImageGraphicsContext(pBitmap, &pG);
 
// here to work with graphics
 
// here to check the image ... save to file
CLSID pngClsid;    
GetEncoderClsid(L"image/png", &pngClsid);
GpBitmap* pBitmap2;
result = Gdiplus::DllExports::GdipCreateBitmapFromGraphics(60, 60, pG, &pBitmap2);

pBitmap refers to bitmap (picture of window). I want to do rotation with the pBitmap2 but I cannot find out

1) how to insert the bitmap (picture) into the bitmap2 (empty one - just created)

2) how to get the graphics back to bitmap.

3) Edit: I think 1) is not what I really need. I need to convert graphics to bitmap. BUt when I call
GdipCreateBitmapFromGraphics I got blank file! So pls how to convert the graphics to bitmap?

 

If you know how to do it in AHK, pls help and I will find out how to do it in C++. If you know how to do it in C++ help me with C++ version directly.



noname
  • Members
  • 650 posts
  • Last active:
  • Joined: 12 Nov 2011

There is a very good example in the function made by Learning One:

 

http://www.autohotke...-58#entry455137

 

Basically it is like this:

 

pBitmap2 := Gdip_CreateBitmap(RWidth, RHeight)    ; create new bitmap
G2 := Gdip_GraphicsFromImage(pBitmap2)               ;get graphics paramaters from it
Gdip_TranslateWorldTransform(G2, xTranslation, yTranslation)   ; set up rotation in the graphics object
Gdip_RotateWorldTransform(G2, Angle)
Gdip_DrawImage(G2, pBitmap, 0, 0, Width, Height)   ;if you draw  something in the graphics you find this(rotated) in pBitmap2

 

Learning one 's function will return the pBitmap2 (that has the rotated bitmap in  it but adapted width/height)

 

Hope it helps...

 

 

This is a complete example i copied/adapted from learning one (it downloads an image and set it in a GUI using setimage) it uses a lot of interesting code!

;===Auto-execute========================================================================
IfNotExist, % A_ScriptDir "\girl.png"
URLDownloadToFile, http://i.imgur.com/Ec3w1Yq.png, % A_ScriptDir "\girl.png"

pToken := Gdip_Startup()
pBitmapA := Gdip_CreateBitmapFromFile(A_ScriptDir "\girl.png")
pBitmapB := Gdip_RotateBitmap(pBitmapA, 90, 0) ; rotates bitmap for 90 degrees. Does not dispose of pBitmapA.

Gdip_GetImageDimensions(pBitmapA, WidthA, HeightA), Gdip_GetImageDimensions(pBitmapB, WidthB, HeightB)

Gui 1:Add, Picture, % "x2 y2 w" WidthA " h" HeightA " BackgroundTrans 0xE vPictureA"
Gui 1:Add, Picture, % "x" WidthA+4 " y2 w" WidthB " h" HeightB " BackgroundTrans 0xE vPictureB"

SetBitmap2Pic(pBitmapA, "PictureA"), SetBitmap2Pic(pBitmapB, "PictureB")
Gdip_DisposeImage(pBitmapA), Gdip_DisposeImage(pBitmapB), Gdip_Shutdown(pToken)

GuiW := WidthA + WidthB + 6, GuiH := (HeightA > HeightB) ? HeightA : HeightB +4
Gui 1:Show, % "w" GuiW " h" GuiH, Original & Rotated
return


;===Functions===========================================================================
;#Include Gdip.ahk ; by Tic www.autohotkey.com/community/viewtopic.php?f=2&t=32238

Gdip_RotateBitmap(pBitmap, Angle, Dispose=1) { ; returns rotated bitmap. By Learning one.
Gdip_GetImageDimensions(pBitmap, Width, Height)
Gdip_GetRotatedDimensions(Width, Height, Angle, RWidth, RHeight)
Gdip_GetRotatedTranslation(Width, Height, Angle, xTranslation, yTranslation)

pBitmap2 := Gdip_CreateBitmap(RWidth, RHeight)
G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
Gdip_TranslateWorldTransform(G2, xTranslation, yTranslation)
Gdip_RotateWorldTransform(G2, Angle)
Gdip_DrawImage(G2, pBitmap, 0, 0, Width, Height)

Gdip_ResetWorldTransform(G2)
Gdip_DeleteGraphics(G2)
if Dispose
Gdip_DisposeImage(pBitmap)
return pBitmap2
} ; http://www.autohotkey.com/community/viewtopic.php?p=477333#p477333
/*
;Examples:
pRotatedBitmap := Gdip_RotateBitmap(pBitmap, 45) ; rotates bitmap for 45 degrees. Disposes of pBitmap.
pRotatedBitmap := Gdip_RotateBitmap(pBitmap, 77) ; rotates bitmap for 77 degrees. Disposes of pBitmap.
pRotatedBitmap := Gdip_RotateBitmap(pBitmap, -22, 0) ; rotates bitmap for -22 degrees. Does not dispose of pBitmap.
*/

SetBitmap2Pic(pBitmap,ControlID,GuiNum=1) { ; sets pBitmap to picture control (which must have 0xE option and should have BackgroundTrans option). By Learning one.
GuiControlGet, hControl, %GuiNum%:hwnd, %ControlID%
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap), SetImage(hControl, hBitmap), DeleteObject(hBitmap) 
GuiControl, %GuiNum%:MoveDraw, %ControlID% ; repaints the region of the GUI window occupied by the control
}

winXP  and ahk unicode