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 10x10 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 PictureBox
on your form, and set its Image
property like this (using the Bitmap
from your code above):
pictureBox1.Image = bmp;
Also, you'll want some using
blocks in your code:
using (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);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(source, newRect, toZoom, GraphicsUnit.Pixel);
}
pictureBox1.Image = bmp;
}
Note that there is no using
block with bmp
- this is because you're setting it as the PictureBox's Image property. The using
block 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.
it will be copied and not displayed.
精彩评论