0

I want to create a rotating wheel in c# form and some pictures get stretched for no apparent reason.

I tried looking into file properties as some images get stretched and some are just fine but i failed to find anything.

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            angle = (int)(angle * szybkosc);
            //
            //kolo1
            Bitmap bit_kolo1 = new Bitmap(kolo1.Width, kolo1.Height);
            Graphics gkolo1 = Graphics.FromImage(bit_kolo1);
            gkolo1.TranslateTransform(bit_kolo1.Width / 2, bit_kolo1.Height / 2);
            gkolo1.RotateTransform(angle);
            gkolo1.TranslateTransform(-bit_kolo1.Width / 2, -bit_kolo1.Height / 2);
            gkolo1.InterpolationMode = InterpolationMode.HighQualityBicubic;

            gkolo1.DrawImage(kolo1, 0,0);

EDIT 1: I noticed that is only happenes with pictures that i previously cropped using gimp. Still dont know how to fix it tho

Kajquo
  • 53
  • 5
  • You need to control the dpi values of the images! Graphics defaults to the srceen dpi but files can come with any dpi values. DrawImage will onor them; if you don't want that you can change them before drawing. See [here](https://stackoverflow.com/questions/26525965/rotate-a-graphics-bitmap-at-its-center/26527737?r=SearchResults&s=1|32.2799#26527737) – TaW May 30 '19 at 08:32
  • @TaW doesnt seem to work – Kajquo May 30 '19 at 09:27
  • _doesn't work_ is not a useful comment. Show the code you tried! What are the dpi values of the various files? – TaW May 30 '19 at 09:30

1 Answers1

0

When using Graphics.DrawImage do always specify the exact size because in some cases the image will be stretched/shrunken otherwise:

gkolo1.DrawImage(kolo1, 0, 0, kolo1.Width, kolo1.Height); // or whatever desired size

If you don't specify the size the default behavior considers the HorizontalResolution and VerticalResolution properties, which are arbitrary values in DPI and it seems Gimp does not adjust these values accurately when you crop the image so it will be stretched (to the original size maybe?).

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • Setting the correct dpi values will avoid quality loss that comes with stretching and should be enough if the sizes are the same, so I would only use this as a last resort if the sizes are in fact wrong. But if they are he made a mistake when cropping the images. – TaW May 30 '19 at 10:36