Image shows in the background after rotating

Ahmad

I wrote a function that rotated an image, to make it fast (which is really important for rotating hundreds of photos) I didn't make a new bitmap every time I rotate the image. But, this resulted the old photo to appear in the background, how could I fix that without creating a new bitmap which will slow down everything!

    public static Image RotateImage(Image img, float rotationAngle)
    {
        using (Graphics graphics = Graphics.FromImage(img))
        {
            graphics.TranslateTransform((float)img.Width / 2, (float)img.Height / 2);
            graphics.RotateTransform(rotationAngle);
            graphics.TranslateTransform(-(float)img.Width / 2, -(float)img.Height / 2);
            graphics.DrawImage(img, new Point(0, 0));
        }
        return img;
    }
TaW

I don't think this is possible without extra code.

Graphics is associated with the image, so drawing will change the image.

So, you will need to have a 2nd image. Creating an empty one (or an exact copy) is not so slow..

Think of of it this way: Where could the old pixels come from when you are at the same time changing them? So you need to have two buffers. (But yes, internally, there is already a 2nd buffer or else the results would be even a lot weirder. But you can't control the use of it..)

If you really feel the need to avoid a 2nd image you can create a GraphicsPath or a Polygon that covers all but the rotated image and fill it with your background color..

But since rotating an image will need more space to accomodate the rotated corners you probably need a 2nd, larger image anyway..

Update: Here is an example of how to clear/crop the area outside the rotated imgae. It uses a GraphicsPath to which I first add a huge and then the target rectangle. This way a hole is cut and only the outside area is filled:

public static Image RotateImage(Image img, float rotationAngle)
{
    using (Graphics graphics = Graphics.FromImage(img))
    {
        graphics.TranslateTransform((float)img.Width / 2, (float)img.Height / 2);
        graphics.RotateTransform(rotationAngle);
        graphics.TranslateTransform(-(float)img.Width / 2, -(float)img.Height / 2);
        graphics.DrawImage(img, new Point(0, 0));

        GraphicsPath gp = new GraphicsPath();
        GraphicsUnit gu = GraphicsUnit.Pixel;
        gp.AddRectangle(graphics.ClipBounds);
        gp.AddRectangle(img.GetBounds(ref gu));
        graphics.FillPath(Brushes.White, gp);
    }
    return img;
}

Note that you can't use a transparent brush as GDI+ will not paint full transparency. Instead you would need to

  1. Set the CompositingModefrom its default SourceOver to SourceCopy;
  2. Fill with a very distinct color, not in your image, maybe Fuchsia
  3. Use MakeTransparent.

graphics.CompositingMode = CompositingMode.SourceCopy;
..
graphics.FillPath(Brushes.Fuchsia, gp);
((Bitmap)img).MakeTransparent(Color.Fuchsia);

Note that not all applications will show transparency well.. Photoshop, of course, does..:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Image rotating after CIFilter

imagecopyresampled after rotating the image

Rotating stripes in SVG background image

Rotating background image opposite to direction of div

Rotating a small image on a background image places the small image in the wrong location

Saving edited image after zooming, rotating and panning

Rotating a transparent png image over a background image with android API=8

Specify background color when rotating an image using OpenCV in Python

Rotating text with knockout effect; background image should not rotate

jQuery - Sync Rotating Background-Image With Current Slide Indicator

How to remove black background that comes after rotating bitmap?

Image not rotating

Blockly workspace background shows random image?

Cloudfront distributed image shows when entered as regular image, but not as a background image

How to keep the image's quality after rotating in C#?

Calculate pixels belonging to corners of rectangle after rotating image

Keep an image visible inside a cardView on a recyclerView after rotating device

Crop background image after positioning

Background image not working after deployment

Change background image after purchased

CSS :after tag with background image and %

How do I make the background image to fill the white spaces (on div) while rotating the image CSS or Javascript?

RecyclerView shows wrong image after fast scroll

Uiimageview shows nothing after get image

Android: Background drawable not rotating when rotating ToggleButton

background-image shows correctly on desktop browsers, not shown on mobile browsers

css "background-image" shows unwanted border, img "src" does not

Why does padding affect whether or not a background image shows up?

css background image shows infront of my other code