Add copyright information to images (C #) [Post]

Source: Internet
Author: User
Nowadays, more and more websites like to add copyrighted information to images uploaded by users. Do not think that it is added with image processing software such as PhotoShop. In fact, we only need to write a short piece of code, you can achieve this function.

The principle of adding copyright information is actually quite simple: the graphics class objects are obtained through images. This class has a drawstring () method that can write information to images and even make various effects, such as watermarks and transparent backgrounds. The last step is to save the image.

Create a Windows application project and design the interface.

The code for adding copyright information is as follows:

Code Generation:
// Create a bitmap

Bitmap bitmap = new Bitmap (this. picturebox2.width, this. picturebox2.height, system. Drawing. imaging. pixelformat. format24bpprgb );

// Obtain the canvas Based on the bitmap

Graphics G = graphics. fromimage (Bitmap );

// Clear the canvas and fill it with transparent colors

G. Clear (color. Transparent );

// Draw another image to the canvas

G. drawimage (this. picturebox1.image, 0, 0 );

// Write the copyright information to the image.

G. drawstring (this. textbox2.text, new font ("", 15), new solidbrush (color. Red), new rectangle (20, 20, 100,100 ));

// Display

This. picturebox2.image = bitmap;

// Save the image

Bitmap. Save ("C: // abc.bmp", system. Drawing. imaging. imageformat. BMP );

By the way, the "select" button click event program:

Private void button#click (Object sender, system. eventargs E)

{

If (this. openfiledialog1.showdialog () = dialogresult. OK)

{

If (this. openfiledialog1.filename. Length = 0)

{

MessageBox. Show ("select image", "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 ("the file you selected is not an identifiable image format", "error", messageboxbuttons. OK, messageboxicon. Error );

}

Finally

{

FS. Close ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.