Search code examples
c#drawingtransformpictureboxrectangles

Tranfer Contents inside a Rotated Polygon to Bitmap


I'm using C# WinForms. The rotated polygon is drawn on a picturebox. The width and height of the rotated polygon is 101, 101. Now, I want to transfer the contents of rotated polygon to new bitmap of size 101,101

enter image description here

I tried to paint pixels of the rectangle using this code

 for (int h = 0; h < pictureBox1.Image.Height; h++)
        {
            for (int w = 0; w < pictureBox1.Image.Width; w++)
            {
                if (IsPointInPolygon(rotatedpolygon, new PointF(w, h)))
                {
                    g.FillRectangle(Brushes.Black, w, h, 1, 1); //paint pixel inside polygon
                }                   
            }
        }

The pixels are painted in the following manner:enter image description here

Now, how do I know which location on the rotated rectangle goes to which location in the new bitmap. That is how do i translate pixel co-ordinates of rotated rectangle to new bitmap.

or simply put, is it possible to map rows and columns from rotated rectangle to new bitmap as shown below?

enter image description here

Sorry, if the question is not clear.


Solution

  • What you asking to do is not literally possible. Look at your diagram:

    enter image description here

    On the left side, you've drawn pixels that are themselves oriented diagonally. But, that's not how the pixels actually are oriented in the source bitmap. The source bitmap will have square pixels oriented horizontally and vertically.

    So, let's just look at a little bit of your original image:

    enter image description here

    Consider those four pixels. You can see in your drawing that, considered horizontally and vertically, the top and bottom pixels overlap the left and right pixels. More specifically, if we overlay the actual pixel orientations of the source bitmap with your proposed locations of source pixels, we get something like this:

    enter image description here

    As you can see, when you try to get the value of the pixel that will eventually become the top-right pixel of the target image, you are asking for the top pixel in that group of four. But that top pixel is actually made up of two different pixels in the original source image!

    The bottom line: if the visual image that you are trying to copy will be rotated during the course of copying, there is no one-to-one correspondence between source and target pixels.

    To be sure, resampling algorithms that handle this sort of geometric projection do apply concepts similar to that which you're proposing. But they do so in a mathematically sound way, in which pixels are necessarily merged or interpolated as necessary to map the square, horizontally- and vertically-oriented pixels from the source, to the square, horizontally- and vertically-oriented pixels in the target.

    The only way you could get literally what you're asking for — to map the pixels on a one-to-one basis without any change in the actual pixel values themselves — would be to have a display that itself rotated.

    Now, all that said: I claim that you're trying to solve a problem that not only is not solvable, but also is not worth solving.

    Display resolution is so high on modern computing devices, even on the phone that you probably have next to you or in your pocket, that the resampling that occurs when you rotate bitmap images is of no consequence to the human perception of the bitmap.

    Just do it the normal way. It will work fine.