Jump to content

GDI+: rotate part of an image


flet
 Share

Recommended Posts

Hi. I'm trying to grasp the possibilities of GDI+, but I'm confused about some things. Maybe someone can explain and correct me where I'm wrong. What's the difference between an image, a graphic and a bitmap? What I understand is that an image is an image which can be vector-based or raster-based and that a bitmap is only raster-based. But where does a graphic fit in?

In my script, which is solely for learning purposes, I'm trying to do the following: I have a bitmap picture which contains two cartoon carrots (see attachment), the left one positioned at an angle, like '/' , and the right one stands up straight, like '|' . Now I want to rotate the left one 25 degrees so it also stands up straight. But I can't figure out how to do this.

This is what I have so far:

#include <GDIPlus.au3>

$image_in = "carrot.bmp"
$image_out = "rotatedcarrot.bmp"

_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($image_in)
; rotated carrot is at:   x-left: 4, y-top: 3, width: 15, height: 21
$hBitmap = _GDIPlus_BitmapCreateFromFile($image_in)
$hSelection = _GDIPlus_BitmapCloneArea($hBitmap, 4, 3, 15, 21)
; save image to test if selection is correct
_GDIPlus_ImageSaveToFile($hSelection, @ScriptDir & "\selectedcarrot.bmp")

$hGraphic =  _GDIPlus_ImageGetGraphicsContext($hSelection)

; trying to rotate here; selected carrot should rotate around its center
$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, Floor(15/2), Floor(21/2))
_GDIPlus_MatrixRotate($hMatrix, 25)

_GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)
_GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 4, 3)

; save the image
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\" & $image_out)

; clean up
_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()

I don't need a GUI or something. Just working with the images will do.

Who can enlighten me?

Thanks in advance,

- flet

carrot.bmp

Link to comment
Share on other sites

As far as I know you cannot rotate an area of an image, only the whole image. What you can do is to rotate one image over a fixed image.

The graphic is here an handle where you can display several graphic elements like bitmaps, draw brushes and pens, etc.

Look in the forum for several GDI+ examples.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

The only thing that i can tell you is to watch others GDI examples about the uses of matrices because after the tranlation you have to draw on a different part of the graphic. alse figure out wich image was in wich graphic.

EDIT:

This is my try. it can be better, still have to clean the area where the rotated carrot will be drawn.

#include <GDIPlus.au3>

$image_in = "carrot.bmp"
$image_out = "rotatedcarrot.bmp"

_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($image_in)
; rotated carrot is at:   x-left: 4, y-top: 3, width: 15, height: 21
$hBitmap = _GDIPlus_BitmapCreateFromFile($image_in)
$hSelection = _GDIPlus_BitmapCloneArea($hBitmap, 4, 3, 15, 21)
; save image to test if selection is correct
_GDIPlus_ImageSaveToFile($hSelection, @ScriptDir & "\selectedcarrot.bmp")
; trying to rotate here; selected carrot should rotate around its center
$hGraphic =  _GDIPlus_ImageGetGraphicsContext($hSelection)
$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, Floor(15/2), Floor(21/2))
_GDIPlus_MatrixRotate($hMatrix, 48)
_GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)
_GDIPlus_GraphicsDrawImage($hGraphic, $hSelection, -Floor(15/2)/2, -Floor(21/2)/2)
; save image to test if rotation is correct
_GDIPlus_ImageSaveToFile($hSelection, @ScriptDir & "\rotated1carrot.bmp")
; Drawing the rotated image over the Image
$hGraphic2 =  _GDIPlus_ImageGetGraphicsContext($hImage)
_GDIPlus_GraphicsDrawImage($hGraphic2, $hSelection, 0, 0)
; save the image
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\" & $image_out)
; clean up
_GDIPlus_GraphicsDispose($hGraphic2)
_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_GraphicsDispose($hGraphic)
 _GDIPlus_BitmapDispose($hSelection)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
Edited by monoscout999
Link to comment
Share on other sites

OK. Thanks for your quick replies. I'll try to rotate the selected carrot image then, and place it on top of the whole image. I think that that's my best option. I'll be back if I can't figure it out myself after viewing the examples in this forum.

EDIT:

Whoa that was fast Monoscout999! An example even before I finished writing my reply.

Thanks man

Edited by flet
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...