简体   繁体   中英

How do I draw an image reflection in WinForms?

I want do draw a reflection in C# (WinForms) of an image, so I need to be able to flip the image horizontally. I know I can do this with image.RotateFlip, but the problem with this approach is that I have to flip the image twice so I can draw it again the right side up on the next paint. Doing this twice per paint per image seems to be slow.

I would like to do the flip when I draw the image so I only have to flip it once, but I can't find any way to do this. Is this possible?

Another approach I've considered is somehow flipping the graphics object, drawing the image normally, and then flipping the graphics object back so that the next paint is correct. If this is faster than flipping the image twice, is it possible to do?

Also, I don't want to keep 2 images in memory, so I can't copy the image and flip the clone.

Got this code from here and check out and see if it is of any help.

using System;
using System.Drawing;
using System.Windows.Forms;

class ImageReflection: Form
{
     Image image = Image.FromFile("Color.jpg");

     public static void Main()
     {
          Application.Run(new ImageReflection());
     }
     public ImageReflection()
     {
          ResizeRedraw = true;

     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          int cxImage = image.Width;
          int cyImage = image.Height;

          grfx.DrawImage(image, cx / 2, cy / 2,  cxImage,  cyImage);
          grfx.DrawImage(image, cx / 2, cy / 2, -cxImage,  cyImage);
          grfx.DrawImage(image, cx / 2, cy / 2,  cxImage, -cyImage);
          grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, -cyImage);
     }
}

edit

something like this too?

 
 
 
 
  
  
   draw(new Bitmap (img).rotateflip(param))
 
 
  

ok, rotateflip doesn't return an image

looking at this , only one flip is enough from the rotateflip. no?

RotateNoneFlipNone  Specifies no rotation and no flipping.
Rotate90FlipNone    Specifies a 90-degree rotation without flipping.
Rotate180FlipNone   Specifies a 180-degree rotation without flipping.
Rotate270FlipNone   Specifies a 270-degree rotation without flipping.
RotateNoneFlipX Specifies no rotation followed by a horizontal flip.
Rotate90FlipX   Specifies a 90-degree rotation followed by a horizontal flip.
Rotate180FlipX  Specifies a 180-degree rotation followed by a horizontal flip.
Rotate270FlipX  Specifies a 270-degree rotation followed by a horizontal flip.
RotateNoneFlipY Specifies no rotation followed by a vertical flip.
Rotate90FlipY   Specifies a 90-degree rotation followed by a vertical flip.
Rotate180FlipY  Specifies a 180-degree rotation followed by a vertical flip.
Rotate270FlipY  Specifies a 270-degree rotation followed by a vertical flip.
RotateNoneFlipXY    Specifies no rotation followed by a horizontal and vertical flip.
Rotate90FlipXY  Specifies a 90-degree rotation followed by a horizontal and vertical flip.
Rotate180FlipXY Specifies a 180-degree rotation followed by a horizontal and vertical flip.
Rotate270FlipXY Specifies a 270-degree rotation followed by a horizontal and vertical flip.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM