Add copyright information to a picture (C #)

Source: Internet
Author: User
Tags transparent color
Add copyright information to a picture (C #)



Now more and more websites like to upload the user's pictures plus the copyright information of the site, do not think that is using Photoshop and other picture processing software added to, in fact, we just write a small piece of code, you can achieve this function.



The principle of adding copyright information is quite simple: get the object of graphics class by picture, the class has a drawstring () method can write the information to the picture, even can make a variety of effects, such as watermark, background transparent and so on. The final save picture is done.



We create a Windows Application project, and the interface design is as follows:





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 a canvas from a bitmap

Graphics g=graphics.fromimage (bitmap);

Empty the canvas and fill with a transparent color

G.clear (color.transparent);

Draw another picture onto the canvas

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

Write the copyright message to the picture.

g.DrawString (This.textbox2.text,new Font ("bold"), New SolidBrush (color.red), New Rectangle (20,20,100,100));

Show

This.picturebox2.image=bitmap;

Save picture

Bitmap. Save ("C:\\abc.bmp", SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);



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

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 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 ("The file you selected is not a recognizable picture 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.