views:

294

answers:

2

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.

+1  A: 
Fredou
To paint the image with a reflection, I have to (1)paint the image right side up, then I have to (2)flip it to paint the reflection, then I have to flip it back so that the next paint step (1) will paint it right side up. This is because the actual Image instance is now flipped. Does that make sense?
NotDan
is it an image or bitmap? the object?
Fredou
It's a System.Drawing.Bitmap object.
NotDan
updated my answer
Fredou
+3  A: 

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);
     }
}
Shoban
That does it, I didn't realize you could do a negative height...
NotDan