Resize image from graphics?

I am trying to resize an image after copying from the screen and cannot figure out how to do this. The tutorials I read recommend using Graphics.DrawImage to resize the image, but when I run this code, it doesn't change.

Bitmap b = new Bitmap(control.Width, control.Height); Graphics g = Graphics.FromImage(b); g.CopyFromScreen(control.Parent.RectangleToScreen(control.Bounds).X, control.Parent.RectangleToScreen(control.Bounds).Y, 0, 0, new Size(control.Bounds.Width, control.Bounds.Height), CopyPixelOperation.SourceCopy); g.DrawImage(b, 0,0,newWidth, newHeight); 

Any help would be appreciated, thanks!

+4
source share
2 answers

Try it. Graphics will not "replace" the image when using DrawImage - it draws the input image at its source, which is the same as the image you are trying to draw.

Perhaps this is a shorter way to do this, but .....

 Bitmap b = new Bitmap(control.Width, control.Height); using (Graphics g = Graphics.FromImage(b)) { g.CopyFromScreen(control.Parent.RectangleToScreen(control.Bounds).X, control.Parent.RectangleToScreen(control.Bounds).Y, 0, 0, new Size(control.Bounds.Width, control.Bounds.Height), CopyPixelOperation.SourceCopy); } Bitmap output = new Bitmap(newWidth, newHeight); using (Graphics g = Graphics.FromImage(output)) { g.DrawImage(b, 0,0,newWidth, newHeight); } 
+6
source

Is there a reason you can't just use the PictureBox control? This control will take care of you.

0
source

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


All Articles