Graphics.DrawImage does not draw correctly

Petri X 41 Reputation points
2020-10-24T17:22:55.54+00:00

Hi,
I'm a bit lost :(

I have the following code section:

private Bitmap rotateImage(Bitmap rotateMe, float angle)  
            {  
                Bitmap rotatedImage = new Bitmap(rotateMe.Width, rotateMe.Height);  
      
                using (Graphics myG = Graphics.FromImage(rotatedImage))  
                {  
                    Pen blackPen = new Pen(Color.Green, 2);  
                    myG.DrawImage(rotateMe, new Point(0, 0));  
                    myG.DrawLine(blackPen, 200, 0, 50, 50);  
                }  
      
                return rotatedImage;  
                //return rotateMe;  
            }  

Originally this was taken from how to make analog clock. Basic idea is to rotate several pictures to make clock shown the time. There were other lines as well, but I those are not impacting to problem itself.

The image coming from "rotateMe" is 200px high and 200px width and looks like following:
34763-sekuntti.png
(the background is transparent)

But when above code draw the image, it looks like this:
34814-incorrectsec.png
The green line I made for myself to understand where is the point 200x0, and "label" are just describing the size of PictureBox on canvas.

If I switch the return to returning the rotateMe bitmap, then the image is drew correctly:
34705-correctsec.png

Anyone have a clue what I do wrong with the draw? Why it is extending the image size?

Developer technologies Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.