Nowadays, more websites like to add pictures uploaded by users to the website copyright information, don't think that it is to use the picture processing software such as Photoshop, in fact, we can implement this function as long as we write a small code. .
The principle of adding copyright information is actually quite simple: Get the object of the Graphics class through the image, which has a DrawString () method to write the information to the picture, even make a variety of effects, such as watermark, background transparency Wait. Finally, save the picture, you will have it.
We created a Windows application project, the interface design is shown:
The code to add copyright information is as follows:
// Create a bitmap
Bitmap Bitmap = New Bitmap (this.PictureBox2.width, this.picturebox2.height, system.drawing.imaging.pixelformat.format24bpprgb);
/ / Get the canvas according to bitmap
Graphics g = graphics.fromimage (bitmap);
// Empty the canvas and fill it with transparent color
g.clear (color.transparent);
// draw another picture to the canvas
g.drawimage (this.PictureBox1.image, 0,0);
// Write copyright information to the picture.
g.drawstring (this.textbox2.text, new font ("black body", 15), new solidbrush (color.red), New Rectangle (20, 20, 100, 100));
//display
This.PictureBox2.Image = bitmap;
//save Picture
Bitmap.save ("c: //abc.bmp", system.drawing.image.imageformat.bmp);
By the way, click the "Select" button Click the event:
Private void Button1_Click (Object Sender, System.Eventargs E)
{
IF (this.openfiledialog1.showdialog () == DialogResult.ok)
{
IF (this.openfiledialog1.filename.length == 0)
{
MessageBox.show ("Please select the picture", "Error", MessageBoxButtons.ok, MessageBoxicon.ERROR);
Return;
}
THIS.TEXTBOX1.TEXT = this.openfiledialog1.filename;
FileStream Fs = New FileStream (this.openfiledialog1.filename, filemode.open, fileaccess.read);
Try
{
This.PictureBox1.Image = image.fromstream (fs);
}
Catch (Exception)
{
MessageBox.show ("Your selected file is not recognizable picture format", "error", messageboxbuttons.ok, messageboxicon.error;
}
Finally
{
fs.close ();
}
}
}