Last post Aug 04, 2008 11:32 AM by Sakshi_M
Jun 23, 2005 10:00 AM|BGT|LINK
Jun 23, 2005 03:11 PM|JasonFollas|LINK
Think about the rotation that's occurring, and be aware of this when you draw the image.
When you rotate an image 90 degrees around it's (0,0) point, the entire image is out of frame after the rotation. Why? Think of putting a thumbtack at the upper left corner of a piece of paper on a bulletin board, and then turning it 90 degrees. Now look
where the original picture was--no part of the rotated picture occupies the same space as the original. (This might not be technically correct in the same sense as the System.Drawing coordinate system, but hopefully it illustrates my point).
To correct this, you need to draw the image displaced so that when it is rotated, it will be in frame.
Consider this sample code:
using (System.Drawing.Image origImg = System.Drawing.Image.FromFile(@"c:\test.jpg"))
using (System.Drawing.Bitmap newImg = new System.Drawing.Bitmap(origImg.Height, origImg.Width))
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(newImg))
System.Drawing.Drawing2D.Matrix mx=new System.Drawing.Drawing2D.Matrix();
gr.DrawImageUnscaled(origImg, 0, -1*origImg.Height);
Here, I'm rotating 90 degrees, but when I draw the original image onto the new image's Graphics, I use the point (0, -1*origImg.Height). After the transform, the image will be properly located within the newImg's region.
Here's the rotation displacements:
0 degrees = (0,0)
90 degrees = (0, -height)
180 degrees = (-width, -height)
270 degrees = (-width, 0) Note: 270 degrees is same as -90 degrees
Jun 27, 2005 03:12 AM|BGT|LINK
Jason, thanks for the quick reply.
The rotation works fine, but the image is also scaled although we use the DrawImageUnscaled method ! The image occupies only 1/4 of the available space. The rest is black.
Any idea ?
Jun 27, 2005 08:51 AM|JasonFollas|LINK
I'm not clear as to what's going on. Maybe if you can reply with some pixel dimensions, it will help me to visualize your problem.
Is it that you have a 400x400 drawing that you're trying to rotate onto a 1600x1600 canvas (per your original post), and need it to scale to fill the new canvas?
Or do you have a 1600x1600 drawing that after rotating, only occupies a 400x400 section?
Jun 27, 2005 09:11 AM|BGT|LINK
The dimension actually doesn't really matter.
Suppose I have a 1600 x 1200 picture. After rotation it should be 1200 x 1600 pixels in case of a 90° rotation.
What happens now is that the canvas is ok (it's a new image with the correct dimension) but the image resides in the upper right corner and is only (approx) 1/4 of the size of the canvas. The rest of the canvas is black. I tried to draw it below.
* | *
* | *
* | My Image *
* | *
* | *
* | *
* | *
* Black Canvas *
Jun 28, 2005 06:56 AM|JasonFollas|LINK
Jun 28, 2005 07:51 AM|BGT|LINK
Jun 28, 2005 10:18 AM|JasonFollas|LINK
Man, that's crazy! I was able to reproduce it, but I'm still trying to understand why it's happening. As a workaround, though, use the DrawImage() method and specify a height and width:
gr.DrawImage(img, 0, -1*img.Height, img.Width, img.Height);
Jul 13, 2005 05:57 AM|BGT|LINK
Jul 14, 2005 05:22 PM|jethompson|LINK
Jun 15, 2008 10:30 AM|blink18jew|LINK
Aug 04, 2008 11:32 AM|Sakshi_M|LINK
Hi Jason and Bart,
Can anyone send me the whole code of image uploading, rotation of it and saving the rotated image into the Database.. Im new to ASP.Net so finding hard to understand the exact picture of wht u people told..
Thanks in Advance,