C #, bitmap gets doubled, why?

I have this code:

public void rotateRocketImage() { Bitmap b = this.rocketImgOriginal; //create a new empty bitmap to hold rotated image Bitmap tempBitmap = new Bitmap(97,97); //make a graphics object from the empty bitmap Graphics g = Graphics.FromImage(tempBitmap); //move rotation point to center of image //g.TranslateTransform(48, 48); //rotate //g.RotateTransform(this.orient); //move image back //g.TranslateTransform(-48, -48); //draw passed in image onto graphics object g.DrawImage(b,0,0); this.rocketImg = tempBitmap; } 

which (with RotateTransform being turned off currently disabled) should just make this.rocketImg equal to this.rocketImg, but somehow it enlarges the image almost twice ... any ideas what might cause it?

Thanks!

enter image description here

edit: here is the drawing code:

  private void timer1_Tick(object sender, EventArgs e) { Invalidate(); } protected override void OnPaint(PaintEventArgs e) { var tempRocket = new Bitmap( rocket.rocketImg ); using (var g = Graphics.FromImage(tempRocket)) { e.Graphics.DrawImage(tempRocket, 150, 150); } } 
+4
source share
5 answers

The bitmap has a resolution setting.

If your bitmap images have different resolutions, you get warped when drawing one image on another.

See the HorizontalResolution and VerticalResolution properties and the SetResolution Bitmap instance method.

Sample code that shows how it works:

  int magnificationIndex = 2; Bitmap tempRocket = new Bitmap("ccc.bmp"); Bitmap tempBitmap = new Bitmap(97, 97); tempBitmap.SetResolution(tempRocket.HorizontalResolution * magnificationIndex, tempRocket.VerticalResolution * magnificationIndex); using (Graphics g = Graphics.FromImage(tempBitmap)) { g.FillRectangle(Brushes.White, 0, 0, 97, 97); g.DrawImage(tempRocket,0,0); } tempBitmap.Save("result.bmp"); 
+3
source

It seems the problem is with the constructor. This is a link to the class definition on MSDN: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx You must either specify a Graphics object or simply set the resolution manually.

Alternatively, you can simply specify the original image in the constructor, and the new object will inhale its properties.

+1
source

As you said, the original image size is 97 X 97 , but you draw it with 150 X 150 , which makes it larger.

+1
source

It is possible that the control in which you draw this has a SizeMode set to Zoom .

0
source

I think you should consider the properties of the rocketImg control. Make sure it does not stretch or change.

0
source

Source: https://habr.com/ru/post/1342125/


All Articles