Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotate Bitmap 1

Status
Not open for further replies.

BBousman1

Programmer
Apr 16, 2008
27
US
On my form I have a section that's created using some code I found online. What it does is basically simulates the Microsoft Magnifier so I can view what's on my extended monitor while only being able to see my primary monitor.

Here's the dilemma I'm facing: On the extended monitor will be a PDF document, it will be rotated clockwise so that the whole page will fit in a portrait style layout. When it is rotated, the Magnifier section on the primary monitor is rotated as well, I need that to be rotated -90 degrees so it appears normal.

Here's the code that takes the constant screen shots:

Dim e As Graphics = Me.CreateGraphics
Dim hr As Integer = Screen.PrimaryScreen.Bounds.Width
Dim vr As Integer = Screen.PrimaryScreen.Bounds.Height

'Get Zoom Percentage
Dim percent As Single = 1
Dim lengthX As Single = 1010
Dim lengthY As Single = 728

'Center Image Around The Mouse
Dim offsetX As Single = (lengthX - 585) \ 2
Dim offsetY As Single = (lengthY - 390) \ 2

'Actual Area To Blit To
Dim blitAreaX As Integer = 1010
Dim blitAreaY As Integer = 728

'New Bitmap & Graphics Object
Dim b As New Bitmap(CInt(blitAreaX), CInt(blitAreaY))
Dim g As Graphics = Graphics.FromImage(b)

'Try to make things faster
g.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
g.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
g.InterpolationMode = Drawing2D.InterpolationMode.Low

'Try to make things faster
e.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
e.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed
e.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
e.InterpolationMode = Drawing2D.InterpolationMode.Low


'DC Of Desktop And Graphics Object
Dim hWndWindow As IntPtr = GetDesktopWindow()
Dim hdcWindow As IntPtr = GetDC(hWndWindow)
Dim hdcGraphics As IntPtr = g.GetHdc()

'BitBlt & Graphics.DrawImage Solution
'------------------------------------------

'BitBlt the Screen (Captures Transparent Windows & Prevents Mirror Effect)

BitBlt(hdcGraphics.ToInt32, 585, 390, blitAreaX, blitAreaY, hdcWindow.ToInt32, Cursor.Current.Position.X - offsetX, Cursor.Current.Position.Y - offsetY, SRCCOPY Or CAPTUREBLT Or NOMIRRORBITMAP)

'Free Memory
ReleaseDC(hWndWindow.ToInt32, hdcWindow.ToInt32)
g.ReleaseHdc(hdcGraphics)

e.DrawImage(b, New Rectangle(0, 0, blitAreaX, blitAreaY), 0, 0, lengthX, lengthY, GraphicsUnit.Pixel)

e.Dispose()
 
You're going to have to load the image into an array then "walk" the array moving one pixel at a time from it's current location to it's new location. I don't know if there is a rotate command built in, but seeing as you are looking for an easy 90^ then thats easy

EX:

Start:
ABCDEF
ABCDEF
ABCDEF
ABCDEF
ABCDEF

End:
FFFFFF
EEEEEE
DDDDDD
CCCCCC
BBBBBB
AAAAAA

You may want to start with a small example like this, using text values or something similar, so you can identify whether or not you are having logic issues.



-Sometimes the answer to your question is the hack that works
 
private Bitmap rotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)b.Width/2, (float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width/2,-(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}

The above is how to rotate a bitmap by any degree in C#. Translation to VB.Net is simple.

I trust this helps as you are passing a bitmap for drawing.

C
 
Have to admit its not mine. I pinched it years ago and just looked it up.

Props to the original author (whoever you are)

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top