Precision in rotating images - c#

I searched a code to rotate an image in C#, and found this to be almost perfect. It does the job while keeping the same Height and Width properties of my old photo and maintaining its quality.
But, could you improve the code to make it more precise. For example when I send an angle of 0.0001 It doesn't seem to rotate it! (I really need to be so precise in this step)
public static Image RotateImage(Image img, float rotationAngle)
{
Bitmap bmp = new Bitmap(img.Width, img.Height);
turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, new Point(0, 0));
gfx.Dispose();
return bmp;
}

Related

How do you rotate a bitmap an arbitrary number of degrees?

I have a bitmap:
Bitmap UnitImageBMP
And I need to rotate it an arbitrary number of degrees. How do I do this? The RotateFlip method will only rotate in increments of 90 degrees.
I did some searching for you and found this:
public static 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
using(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;
}

Image in PictureBox Become Blurry While Rotating

I am trying to rotate an image that is in side of a PictureBox in a C# WinForm. I rotate the image with every tick of the timer1 (Interval = 100). As I am rotating, however, the image becomes blurrier and blurrier. I am not sure if there is a way to fix this. I have enabled the Form to be double buffered.
Here is the code I am using:
public static Image RotateImage(Image img, float rotationAngle)
{
Bitmap bmp = new Bitmap(img.Width, img.Height);
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Graphics gfx = Graphics.FromImage(bmp);
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
gfx.RotateTransform(-rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
gfx.DrawImage(img, new Point(0, 0));
gfx.Dispose();
return bmp;
}
private void timer1_Tick(object sender, EventArgs e)
{
Image bmp = Form1.RotateImage(pictureBox1.Image, 10);
pictureBox1.Image = bmp;
}
Consider keeping your original image around, and each time you rotate, calculate the new image by applying the cumulative rotation to the original image.
What you're seeing is an unavoidable consequence of the rotation, which can only approximate the original image given the finite number of pixels it has to work with. You're seeing approximations of approximations, and they'll just get worse each time you rotate.

How rotate custom moving marker (image) GMap

I have a marker(plane bug) on GMap which moves on a flight path but I want it to rotate when it turns. Is there any way of doing this in GMap C#?
You can use this function to rotate the image of the marker and then just reasign the marker with this image.
public Bitmap RotateImage(Image image, float angle)
{
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics g = Graphics.FromImage(rotatedBmp);
PointF offset = new PointF(image.Width / 2, image.Height / 2);
g.TranslateTransform(offset.X, offset.Y);
g.RotateTransform(angle);
g.TranslateTransform(-offset.X, -offset.Y);
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}
you can just do it like this then
marker.img = RotateImage(innitialImg, xxx);

I'm experiencing unexpected results from Graphics.DrawImage

To reproduce this issue, please create a 2x2 pixel black image in Microsoft Paint, saved as D:\small.png. Then create a new WinForms app in Visual Studio, with a no-margin PictureBox. Then use the following code:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 400;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
f6(e.Graphics);
}
I expect the entire rectangle inside the blue margins be black while the output is as follows:
Why is this happening?
ok, thanks. i din't know about interpolation. now, let's change the code as following:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 200;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.FillRectangle(new SolidBrush(Color.DarkCyan), pictureBox1.ClientRectangle);
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
it produces the following result:
Result3
which is still unacceptable.
i've tried 60x60 images too. the problem is not because it's a 2x2 image. they produce the same effect. the problem is that why GDI+ decides not to fill the entire destRect with the entire srcRect?!
the original problem is that i've a big image tiled in smaller ones. i need adjacent tiles neither overlap nor seam exist between them. in C++, StretchBlt works properly. but it doesn't produce a smooth stretched image.
GDI+'s definition of the source rectangle is a bit odd.
(0,0) in the source image is actually the center of the upper-left pixel in the image. (width-1,height-1) is the center of the lower-right pixel in the image.
That means that the upper-left pixel is the rectangle from (-0.5,-0.5) to (0.5,0.5), and the lower-right pixel is the rectangle from (width-1.5,height-1.5) to (width-0.5,height-0.5). Thus, your source rectangle is actually outside the image by 0.5 pixels to the right and bottom.
So, you actually need a source rectangle of (-0.5, -0.5, img.Width, img.Height).
I guess you can also try setting PixelOffsetMode as Hans suggests. That would actually make sense of the behavior, but I wouldn't have expected it to apply to source rectangles.
Looks like the image was resized using bicubic interpolation. The normal Bicubic sizing algorithm normally requires 16 pixels to interpolate one, but you only have 4 in the image, so the remaining 12 pixels are never set, staying white.
Try changing the resizing method to nearest-neighbor. This is done by setting the InterpolationMode property of your Graphics object g to InterpolationMode.NearestNeighbor:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 400;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.ImterpolatonMode = InterpolationMode.NearestNeighbor;
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}

C# rotate bitmap 90 degrees

I'm trying to rotate a bitmap 90 degrees using the following function. The problem with it is that it cuts off part of the image when the height and width are not equal.
Notice the returnBitmap width = original.height and it's height = original.width
Can anyone help me solve this issue or point out what I'm doing wrong?
private Bitmap rotateImage90(Bitmap b)
{
Bitmap returnBitmap = new Bitmap(b.Height, b.Width);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
g.RotateTransform(90);
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
What about this:
private void RotateAndSaveImage(String input, String output)
{
//create an object that we can use to examine an image file
using (Image img = Image.FromFile(input))
{
//rotate the picture by 90 degrees and re-save the picture as a Jpeg
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
The bug is in your first call to TranslateTransform:
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
This transform needs to be in the coordinate space of returnBitmap rather than b, so this should be:
g.TranslateTransform((float)b.Height / 2, (float)b.Width / 2);
or equivalently
g.TranslateTransform((float)returnBitmap.Width / 2, (float)returnBitmap.Height / 2);
Your second TranslateTransform is correct, because it will be applied before the rotation.
However you're probably better off with the simpler RotateFlip method, as Rubens Farias suggested.
I came across and with a little modification I got it to work. I found some other examples and noticed something missing that made the difference for me. I had to call SetResolution, if I didn't the image ended up the wrong size. I also noticed the Height and Width were backwards, although I think there would be some modification for a non square image anyway. I figured I would post this for anyone who comes across this like I did with the same problem.
Here is my code
private static void RotateAndSaveImage(string input, string output, int angle)
{
//Open the source image and create the bitmap for the rotatated image
using (Bitmap sourceImage = new Bitmap(input))
using (Bitmap rotateImage = new Bitmap(sourceImage.Width, sourceImage.Height))
{
//Set the resolution for the rotation image
rotateImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
//Create a graphics object
using (Graphics gdi = Graphics.FromImage(rotateImage))
{
//Rotate the image
gdi.TranslateTransform((float)sourceImage.Width / 2, (float)sourceImage.Height / 2);
gdi.RotateTransform(angle);
gdi.TranslateTransform(-(float)sourceImage.Width / 2, -(float)sourceImage.Height / 2);
gdi.DrawImage(sourceImage, new System.Drawing.Point(0, 0));
}
//Save to a file
rotateImage.Save(output);
}
}

Categories