5

I need to flip a bitmap image horizontally in my VB.net web application.

i.e.

enter image description here

I've really searched around, but not come across any simple vb.net method. Does one exist?

Urbycoz
  • 7,247
  • 20
  • 70
  • 108
  • This is another similar SO question http://stackoverflow.com/questions/14694205/flipping-an-image-with-js-jquery – Constanta Apr 16 '13 at 16:06
  • See also http://stackoverflow.com/questions/5768998/how-to-flip-background-image-using-css – nmat Apr 16 '13 at 16:07
  • Yes, I've looked at both of those. But I need to flip my images via vb.net. – Urbycoz Apr 16 '13 at 16:13
  • sorry didn't understand your question then. is there a reason it must be code-behind though. you tagged your question asp.net – Constanta Apr 16 '13 at 16:18
  • You're right- I wasn't clear. I've clarified it now. – Urbycoz Apr 16 '13 at 16:20
  • 1
    Given that this is asp.net, you might do better just serving the original image, and rely on CSS styling to make the client flip the image for you. See this: http://css-tricks.com/snippets/css/flip-an-image/ This would save a _ton_ of work on your server, and therefore help you scale better and reduce page load times. – Joel Coehoorn Apr 16 '13 at 16:24
  • @JoelCoehoorn That's a fair point. It's just that that would take a bit of rewriting, and I feel convinced that there's a simple method that I've come across before. – Urbycoz Apr 16 '13 at 16:33

1 Answers1

3

You get a file reference to the image, load that as a Bitmap, then use the RotateFlip method.

http://msdn.microsoft.com/en-us/library/system.drawing.rotatefliptype.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

In case that link dies as Microsoft is oft known to do:

    bitmap1 = CType(Bitmap.FromFile("C:\Documents and Settings\All Users\" _
        & "Documents\My Music\music.bmp"), Bitmap)
    bitmap1.RotateFlip(RotateFlipType.RotateNoneFlipX)

You may be interested in the Image class (a Bitmap is an Image). You can perform just about any naive operation (like this one), and there are ways to leverage the 3D card to really go crazy. You can take it really far. You need to learn graphics matrices to do custom effects performantly, though.

http://msdn.microsoft.com/en-us/library/k7e7b2kd.aspx

Edit

Comments on the question note performance can be an issue here. Depending on how often an image like this is loaded you likely should cache the results of this, for example by writing it to disk and having the browser actually request the file by the path it was written out to (which will get you whatever IIS caching you've configured for free), or by applying an OutputCache directive to the Controller Action being called here, or whatever - kinda outside the scope of this question.

Also just for the record flipping an image is pretty cheap.

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
  • Thanks. That's the method I was looking for. What do you mean by "pretty cheap"? – Urbycoz Apr 17 '13 at 08:30
  • In terms of CPU cycles, it's pretty cheap - that is, on machines with dedicated graphics cards the GPU does it near-instantly - most of the time lost is just transferring the data out and back. On machines without one, they often have a weak GPU built-in, and even then if they don't have that there are hardware Vector operations that use the underlying Matrix to perform the operation in parallel. So fears of it being slow may not be well-founded, although I do agree performance profiling is always wise, as are good caching strategies. – Chris Moschini Apr 17 '13 at 16:36