C# Paste A Picture (In Graphics)
The thing I want to do is simple. I want to take a picture I already开发者_如何学编程 have and paste it into a blank graphics/picture at a certain point, thus expanding my picture's bounds.
To clarify :
private static Image PasteImage(Image startimage) //start image is a square of Size(30,30)
{
//Create a new picture/graphics with size of (900,900);
//Paste startimage inside the created picture/graphics at Point (400,450)
//Return the picture/graphics which should return a square within a square
}
private static Image PasteImage(Image startimage)
{
int width = Math.Max(900, 400 + startimage.Width);
int height = Math.Max(900, 450 + startimage.Height);
var bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.DrawImage(startimage, 400, 450);
}
return bmp;
}
It's better to get rid of constants in your code and add a couple of additional params:
private static Image PasteImage(Image startimage, Size size, Point startpoint)
{
int width = Math.Max(size.Width, startpoint.X + startimage.Width);
int height = Math.Max(size.Height, startpoint.Y + startimage.Height);
var bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.Clear(Color.Black);
g.DrawImage(startimage, new Rectangle(startpoint, startimage.Size));
}
return bmp;
}
Create an image from the startimage using the following
Graphics.FromImage(startimage);
Draw the image where you want to using
g.DrawImage(...)
精彩评论