Flip image vertically in Compact Framework

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shailendrab
    New Member
    • Mar 2010
    • 4

    Flip image vertically in Compact Framework

    Hi,

    I have been trying to flip an image vertically before saving in disk in AVI format. I really dont know why i need to flip it first before saving, any idea?

    To do this currently i am performing a simple algorithm...

    Code:
            public Bitmap RotateFlip(Bitmap source, RotateFlipType change)
            {
                Bitmap dest = new Bitmap(source.Size.Width, source.Size.Height);
                int MaxWidthIndex = source.Size.Width - 1;
                int MaxHeightIndex = source.Size.Height - 1;
    
                for (int X = 0; X < source.Size.Width; X++)
                {
                    for (int Y = 0; Y < source.Size.Height; Y++)
                    {
                        switch (change)
                        {
                            case RotateFlipType.RotateNoneFlipY:
                                dest.SetPixel(MaxWidthIndex - X, Y, source.GetPixel(MaxWidthIndex - X, MaxHeightIndex - Y));
                                break;
                            default:
                                throw new Exception("Not implemented");
                        }
                    }
                }
    
                return dest;
            }
    This works correctly but has severe performance bottleneck. I guess due to Set & Get pixel methods.

    If you know a better approach and can share code with me, I will really appreciate.

    Thanks,
    Shailendra
    Last edited by tlhintoq; Mar 2 '10, 01:13 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I really dont know why i need to flip it first before saving
      Then why are you doing it?

      before saving in disk in AVI format.
      Excuse me? You're saving your bitmap as an avi? Why?

      Comment

      • shailendrab
        New Member
        • Mar 2010
        • 4

        #4
        Hi,

        Thanks for reply... answers to your questions...


        Then why are you doing it?

        Bcoz i have to. If i dont, when i play recorded video, i see vertically flipped images being displayed.

        Excuse me? You're saving your bitmap as an avi? Why?

        Sorry I made an ambiguous statement. What i meant is, i am getting images from a netwrok camera and i want to record it as video. so to record video i am using AVI library (found a wrapper on code project), which allow me to add images into a video stream. However it is required to perform a flip before saving an image (this is the part i am not sure about).

        I hope i m clear this time :)

        Thanks
        Shailendra

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          I don't know of a faster way to do a image flip than to just parse through the data and change it around. The only way I could say to optimize it would be that you could remove the switch statement in your code. Maybe set up some variables depending on the value...

          Code:
          destImage.SetPixel((direction * x) + startX, (direction * y) + startY);
          Where direction is 1 or -1 and startX/Y is 0 or the width/height accordingly.

          There may be faster ways, but unfortunately I don't know them. Sorry!
          Last edited by GaryTexmo; Mar 8 '10, 02:39 PM. Reason: missed a / on a tag

          Comment

          • shailendrab
            New Member
            • Mar 2010
            • 4

            #6
            Thank you very much Gary for suggesting improvement. I will try this one.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Hmm, additionally it seems there's a built-in method on the Image class.

              The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.


              I don't know if it's any faster or not though, worth a try? I can't help but wonder if you know about this though 'cause you're using the same terminology and the same enum. If it's a matter of having it on a Bitmap class vs. an Image class, I'm pretty sure you can load a bitmap into an Image and vice versa. Some googling might be in order there though.

              Comment

              Working...