HighTechTalks DotNet Forums  

This is how I rotate an bitmap

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss This is how I rotate an bitmap in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
active
 
Posts: n/a

Default This is how I rotate an bitmap - 05-18-2007 , 08:51 AM






I cut some code but this is basically how I rotate a bitmap.

Can't be the best way because it takes too long!

Any help would be appreciated.



'Rotate180

Dim TmpBitmap As New Drawing.Bitmap(OldWidth, OldHeight)

For X As Integer = 0 To OldWidth - 1

For Y As Integer = 0 To OldHeight - 1

TmpBitmap.SetPixel(OldWidth - X - 1, OldHeight - Y - 1,
CType(PictureBoxPic.Image, Drawing.Bitmap).GetPixel(X, Y))

Next Y

Next X

PictureBoxPic.Image.Dispose()

PictureBoxPic.Image = TmpBitmap

End Sub



Reply With Quote
  #2  
Old   
Michael C
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 12:12 AM






" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote

Quote:
I cut some code but this is basically how I rotate a bitmap.

Can't be the best way because it takes too long!

Any help would be appreciated.
SetPixel and GetPixel are slow. There are a few ways to do this, the last is
likely the best:
1) Have a look into lockbits
2) Use a graphics object to paint the bitmap onto itself (or a temp bitmap
if that causes problems)
3) Use Bitmap.Rotate

Seeing no 3 is a single line of code that does everything you want I'd
suggest that would be the best option :-)

Michael




Reply With Quote
  #3  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 05:39 AM



You are so going to kick yourself...

RotateFlip

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote

Quote:
I cut some code but this is basically how I rotate a bitmap.

Can't be the best way because it takes too long!

Any help would be appreciated.



'Rotate180

Dim TmpBitmap As New Drawing.Bitmap(OldWidth, OldHeight)

For X As Integer = 0 To OldWidth - 1

For Y As Integer = 0 To OldHeight - 1

TmpBitmap.SetPixel(OldWidth - X - 1, OldHeight - Y - 1,
CType(PictureBoxPic.Image, Drawing.Bitmap).GetPixel(X, Y))

Next Y

Next X

PictureBoxPic.Image.Dispose()

PictureBoxPic.Image = TmpBitmap

End Sub




Reply With Quote
  #4  
Old   
active
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 07:20 AM



I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is arbitrary.
So I looked up LockBits after I read your replies and am not sure from the
example how to apply it in this situation. I'll read somemore later but if
you happen to know of an example similar to my app it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Thanks



Reply With Quote
  #5  
Old   
Michael C
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 08:26 AM



" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote

Quote:
I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my app
it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.
Look into no 2 in my post. You need to create a graphics object and paint
the bitmap onto itself. You can use graphics.RotateTransform to rotate the
way in which it paints the bitmap onto itself. Now I'm not sure if you can
paint a bitmap onto itself, if you can't then you should create a copy.

Michael




Reply With Quote
  #6  
Old   
active
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 08:46 AM



I have to admit I passed over item 2 because I couldn't understand how
coping would do it, but I see now.

Thanks

"Michael C" <nospam (AT) nospam (DOT) com> wrote

Quote:
" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eCeP5egmHHA.4848 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my app
it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Look into no 2 in my post. You need to create a graphics object and paint
the bitmap onto itself. You can use graphics.RotateTransform to rotate the
way in which it paints the bitmap onto itself. Now I'm not sure if you can
paint a bitmap onto itself, if you can't then you should create a copy.

Michael




Reply With Quote
  #7  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 01:14 PM



You cannot copy a bitmap onto itself.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"Michael C" <nospam (AT) nospam (DOT) com> wrote

Quote:
" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eCeP5egmHHA.4848 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my app
it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Look into no 2 in my post. You need to create a graphics object and paint
the bitmap onto itself. You can use graphics.RotateTransform to rotate the
way in which it paints the bitmap onto itself. Now I'm not sure if you can
paint a bitmap onto itself, if you can't then you should create a copy.

Michael



Reply With Quote
  #8  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 01:19 PM



Try something like this..


Bitmap bm=(Bitmap)Image.FromFile("the image");
Bitmap copy=new Bitmap(bm.Width, bm.Height);
Graphics g=Graphics.FromImage(copy);
Matrix mx=new Matrix();
Matrix.RotateAt(bm.Width/2,bm.Height/2);
g.DrawImage(bm);
copy.Save("file.xxx",ImageFormat.jpg);

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote

Quote:
I have to admit I passed over item 2 because I couldn't understand how
coping would do it, but I see now.

Thanks

"Michael C" <nospam (AT) nospam (DOT) com> wrote in message
news:%23k0vtChmHHA.4316 (AT) TK2MSFTNGP06 (DOT) phx.gbl...
" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eCeP5egmHHA.4848 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my app
it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Look into no 2 in my post. You need to create a graphics object and paint
the bitmap onto itself. You can use graphics.RotateTransform to rotate
the way in which it paints the bitmap onto itself. Now I'm not sure if
you can paint a bitmap onto itself, if you can't then you should create a
copy.

Michael





Reply With Quote
  #9  
Old   
active
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 01:51 PM



I can verify that.
I tried


"Bob Powell [MVP]" <bob (AT) spamkillerbobpowell (DOT) net> wrote

Quote:
You cannot copy a bitmap onto itself.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"Michael C" <nospam (AT) nospam (DOT) com> wrote in message
news:%23k0vtChmHHA.4316 (AT) TK2MSFTNGP06 (DOT) phx.gbl...
" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eCeP5egmHHA.4848 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I signed on early today hoping to beat any replies. I did find that Image
can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my app
it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Look into no 2 in my post. You need to create a graphics object and paint
the bitmap onto itself. You can use graphics.RotateTransform to rotate
the way in which it paints the bitmap onto itself. Now I'm not sure if
you can paint a bitmap onto itself, if you can't then you should create a
copy.

Michael





Reply With Quote
  #10  
Old   
active
 
Posts: n/a

Default Re: This is how I rotate an bitmap - 05-19-2007 , 01:56 PM



I gave it a quick try earlier and it evidently rotated around something
other than the center of the image.

I suspect your code fixes that.

thanks
"Bob Powell [MVP]" <bob (AT) spamkillerbobpowell (DOT) net> wrote

Quote:
Try something like this..


Bitmap bm=(Bitmap)Image.FromFile("the image");
Bitmap copy=new Bitmap(bm.Width, bm.Height);
Graphics g=Graphics.FromImage(copy);
Matrix mx=new Matrix();
Matrix.RotateAt(bm.Width/2,bm.Height/2);
g.DrawImage(bm);
copy.Save("file.xxx",ImageFormat.jpg);

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eke98OhmHHA.4772 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I have to admit I passed over item 2 because I couldn't understand how
coping would do it, but I see now.

Thanks

"Michael C" <nospam (AT) nospam (DOT) com> wrote in message
news:%23k0vtChmHHA.4316 (AT) TK2MSFTNGP06 (DOT) phx.gbl...
" active" <activeNOSPAM (AT) a-znet (DOT) com> wrote in message
news:eCeP5egmHHA.4848 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I signed on early today hoping to beat any replies. I did find that
Image can rotate and flip itself late yesterday and tried it.

But I have another application in which the amount of rotation is
arbitrary. So I looked up LockBits after I read your replies and am not
sure from the example how to apply it in this situation. I'll read
somemore later but if you happen to know of an example similar to my
app it would help.

Anyway, I wanted to reply now so I can say thanks for the replies.

Look into no 2 in my post. You need to create a graphics object and
paint the bitmap onto itself. You can use graphics.RotateTransform to
rotate the way in which it paints the bitmap onto itself. Now I'm not
sure if you can paint a bitmap onto itself, if you can't then you should
create a copy.

Michael







Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.