There is something I am missing. Say I have the following code:
private Bitmap source = new Bitmap (some_stream);
Bitmap bmp = new Bitmap(100,100);
Rectangle newRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Rectangle toZoom= new Rectangle(0, 0, 10, 10);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, newRect, toZoom, GraphicsUnit.Pixel);
My goal is to zoom-in the 10×10 pixels on the top left corner of the source picture. After I created the graphics object g and called DrawImage: the requested rectangle (toZoom) will be copied to bmp, or will it be displayed on the screen? I am a bit confused, can somebody please clarify?
You code will only give you an in-memory bitmap (which won’t automatically be displayed to the screen). A simple way to display this would be to put a 100 x 100
PictureBoxon your form, and set itsImageproperty like this (using theBitmapfrom your code above):Also, you’ll want some
usingblocks in your code:Note that there is no
usingblock withbmp– this is because you’re setting it as the PictureBox’s Image property. Theusingblock automatically calls an object’s Dispose method at the end of the block’s scope, which you don’t want to do since it will still be in use.