Jump to content

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

GDI+ standard library 1.45 by tic


  • Please log in to reply
1385 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
WARNING: This is old post; continuation here.

Nice bar charts example Tic! By the way, here are few functions I made;
- Gdip_ResizeBitmap()
- Gdip_RotateBitmap()
- Gdip_CropBitmap()
- Gdip_CloneBitmap()
Examples are included in comments. If you think they are useful and good enough, feel free to make them part of your Gdip library. Pastebin.
Gdip_ResizeBitmap(pBitmap, PercentOrWH, Dispose=1) { ; returns resized bitmap. By Learning one.
Gdip_GetImageDimensions(pBitmap, origW, origH)
if PercentOrWH contains w,h
{
RegExMatch(PercentOrWH, "i)w(\d*)", w), RegExMatch(PercentOrWH, "i)h(\d*)", h)
NewWidth := w1, NewHeight := h1
NewWidth := (NewWidth = "") ? origW/(origH/NewHeight) : NewWidth
NewHeight := (NewHeight = "") ? origH/(origW/NewWidth) : NewHeight
}
else
NewWidth := origW*PercentOrWH/100, NewHeight := origH*PercentOrWH/100
pBitmap2 := Gdip_CreateBitmap(NewWidth, NewHeight)
G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
Gdip_DrawImage(G2, pBitmap, 0, 0, NewWidth, NewHeight)
Gdip_DeleteGraphics(G2)
if Dispose
Gdip_DisposeImage(pBitmap)
return pBitmap2
}
/*
;Examples:
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, 120) ; resizes to 120%. Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, 50, 0) ; resizes to 50%. Does not dispose of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "w180 h80") ; resizes to width: 180 height: 80 (does not maintain aspect ratio). Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "w200") ; resizes to width: 200, automatically adjusts height (maintains aspect ratio). Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "h50") ; resizes to height: 50, automatically adjusts width (maintains aspect ratio). Disposes of pBitmap.
*/


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
}
/*
;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.
*/

Gdip_CropBitmap(pBitmap, left, right, up, down, Dispose=1) { ; returns cropped bitmap. Specify how many pixels you want to crop (omit) from each side of bitmap rectangle. By Learning one.
Gdip_GetImageDimensions(pBitmap, origW, origH)
NewWidth := origW-left-right, NewHeight := origH-up-down
pBitmap2 := Gdip_CreateBitmap(NewWidth, NewHeight)
G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
Gdip_DrawImage(G2, pBitmap, 0, 0, NewWidth, NewHeight, left, up, NewWidth, NewHeight)
Gdip_DeleteGraphics(G2)
if Dispose
Gdip_DisposeImage(pBitmap)
return pBitmap2
}
/*
;Examples:
pCroppedBitmap := Gdip_CropBitmap(pBitmap, 10, 10, 10, 10) ; crops 10 pixels from each side of bitmap rectangle. Disposes of pBitmap.
pCroppedBitmap := Gdip_CropBitmap(pBitmap, 50, 50, 0, 0) ; crops 50 pixels from left and right side of bitmap rectangle. Disposes of pBitmap.
pCroppedBitmap := Gdip_CropBitmap(pBitmap, 0, 0, 77, 77, 0) ; crops 77 pixels from upper and lower side of bitmap rectangle. Does not dispose of pBitmap.
*/


Gdip_CloneBitmap(pBitmap) { ; returns cloned (duplicated) bitmap. By Learning one.
Gdip_GetDimensions(pBitmap, w, h), pClonedBitmap := Gdip_CloneBitmapArea(pBitmap, 0, 0, w, h)
return pClonedBitmap
}
/*
;Example:
pClonedBitmap := Gdip_CloneBitmap(pBitmap) ; clones (duplicates) bitmap.
*/


JakeW
  • Members
  • 210 posts
  • Last active: Jul 11 2013 05:27 PM
  • Joined: 16 Jul 2011
Well learning, I haven't QUITE used it yet, but I think I will love the ResizeBitmap option you added. The one script I've been using this with will greatly use this, as I can resize any image I want into the size of the drawing board I'm using it on, instead of manually doing it.

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
It seems my post will be lost in the dust :(
Please help!

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
@Nimda:
DC := "Device Context"
<!-- m -->http://msdn.microsof...ibrary/dd183553<!-- m -->

A device context is a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. The remainder of this section is divided into the following three areas.


"You annoy me, therefore I exist."

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
So, I need a DC whenever I want a pen/brush/etc.
When do I need to select stuff?

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007

So, I need a DC whenever I want a pen/brush/etc.
When do I need to select stuff?


So a DC is a device context, which is basically the GDI equivalent of a Graphics pointer (pGraphics) which is GDI+
In GDI you select everything into your DC like brushes etc, similar how you use SetSmoothingMode or SetInterpolationMode to "choose your settings" for your graphics
So the gist of all of this is.....you don't need to care what a DC is when using the GDI+ library as I don't provide any functions for GDI drawing :)

LoveJsPark
  • Members
  • 2 posts
  • Last active: Oct 15 2011 05:11 PM
  • Joined: 01 Oct 2011
dd

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Hi LoveJsPark

Firstly, please use the gdi+ library. I'm not sure if you copied those functions from it, but it makes it easier to know which code you are running if you use the latest version of the library

Also, try using this code and let me know if it works and we can go from there

<!-- m -->http://www.autohotke...pic.php?t=76261<!-- m -->

LoveJsPark
  • Members
  • 2 posts
  • Last active: Oct 15 2011 05:11 PM
  • Joined: 01 Oct 2011
dd

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
did you guys noticed that repeated call of Gdip_Startup & Gdip_Shutdown cause mem leak?
is there only option to use Gdip_Startup once at the beginning of the script?

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008

did you guys noticed that repeated call of Gdip_Startup & Gdip_Shutdown cause mem leak?
is there only option to use Gdip_Startup once at the beginning of the script?

It should only be used once in any script. Why are you using it multiple times?
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
well, i thought you should call Startup whenever you need and shutdown after you finished using gdip functions

The GdiplusShutdown function cleans up resources used by Windows GDI+. Each call to GdiplusStartup should be paired with a call to GdiplusShutdown.


if there is an only option to use these functions without leak - to use GdiplusStartup only at the start of the script, we really don't need to use GdiplusShutdown anywhere in the script, because on process close all this will be cleaned up

if you making any module that use any gdip functions - you must use GdiplusStartup & GdiplusShutdown there, or tell every user who use your module to add GdiplusStartup call at the beginning of the script lol

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010

if you making any module that use any gdip functions - you must use GdiplusStartup & GdiplusShutdown there, or tell every user who use your module to add GdiplusStartup call at the beginning of the script lol

Many libs have a LibName_Init() function, you could put gdip_startup() there. Granted, you'd require a global for the pToken AFAICT :?

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010

Many libs have a LibName_Init() function, you could put gdip_startup() there. Granted, you'd require a global for the pToken AFAICT :?

thats the way, but it not much difference with "tell user to call GdipStartup"

i mean, if you created a class which use some gdip functions, and user should create an instance of it before use - you will need to use GdipStartup in __New() and GdipShutdown in __Delete(), which slowly will eat your memory.
Sure, the leaked memory size not significant... but anyway

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
tic, I have a problem. When I try to use gdip_DrawPie() as below, I keep getting errCode 2, or "Invalid Parameter." I have no idea which parameter though :roll:
I have no idea what's wrong...
#Include <gdip>

if not pToken := gdip_startup(){
   MsgBox gdip couldn't start up.
   goto cleanup
}
width := height := 200

pBitmap := CreateDIBsection(width, height)
pGraphics := gdip_GraphicsFromImage(pBitmap)
pPen := gdip_CreatePen(0x00000000, 2)

[color=red]if errorCode := gdip_DrawPie(pGraphics, pPen, 2, 2, 50, 50, 2, 90){
   MsgBox Pie drawing failed! %errorCode% ; 2[/color]
   goto cleanup
}
If not ErrorCode := gdip_SaveBitmapToFile(pBitmap, (f := (A_ScriptDir . "\pie.jpg")))
	run % f
else MsgBox % errorCode

cleanup:
gdip_DeletePen(pPen)
gdip_DisposeImage(pBitmap)
gdip_DeleteGraphics(pGraphics)
gdip_Shutdown(pToken)