Paint Mirroring in C # Using System.Drawing.Graphics

I wrote a small helper function that performs some kind of drawing operation, which is quite complicated.

I call this function from another class, which sometimes applies transformations to it. Rotation and translation work fine, but now I want to make the helper function draw everything that is mirrored along the y axis.

I tried to use

g.ScaleTransform(0, -1); 

before calling the helper function, but it threw an exception.

So, how can you make a mirror image using the System.Drawing.Graphics object?

+6
source share
2 answers

You need to call

 g.ScaleTransform(1, -1); 

Please note that now your image will be drawn beyond the top edge of the screen. To fix this, you need to call g.TranslateTransform before g.ScaleTransform :

 g.TranslateTransform(0, YourImageHeightHere); g.ScaleTransform(1, -1); 
+11
source

Here's how to do it with BitMap, you can draw an image from the graphics and redraw the graphic with the changed.

  public Bitmap MirrorImage(Bitmap source) { Bitmap mirrored = new Bitmap(source.Width, source.Height); for(int i = 0; i < source.Height; i++) for(int j = 0; j < source.Width; j++) mirrored.SetPixel(i, j, source.GetPixel(source.Width - j - 1, i); return mirrored; } 

Edit: @MattSlay, thanks for being a typo, I fixed it.

+1
source

Source: https://habr.com/ru/post/894436/


All Articles