asp.net picture watermark

Source: Internet
Author: User
Tags net

Using System.IO;
Using System.Drawing.Imaging;

private void Button1_serverclick (object sender, System.EventArgs e)
{
Graphics G=null;
System.Drawing.Image Upimage=null;
System.Drawing.Image Thumimg=null;
System.Drawing.Image Simage=null;
Bitmap Outputfile=null;
Try
{
string extension = Path.getextension (File1.PostedFile.FileName). ToUpper ();
string filename = DateTime.Now.ToString ("Yyyymmddhhmmss");
String smallpath = Server.MapPath (".") + "/smallimg/";
String bigpath = Server.MapPath (".") + "/bigimg/";
int width,height,newwidth,newheight;

System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);
if (! Directory.Exists (Smallpath))
Directory.CreateDirectory (Smallpath);
if (! Directory.Exists (Bigpath))
Directory.CreateDirectory (Bigpath);

Stream upimgfile = File1.PostedFile.InputStream;
String simagefile = Server.MapPath ("a8logo.jpg"); File to be added to the watermark
Simage=system.drawing.image.fromfile (Simagefile);
Upimage= System.Drawing.Image.FromStream (Upimgfile); Uploaded pictures

width = upimage. Width;
Height = upimage. Height;
if (width>height)
{
newwidth=200;
Newheight = (int) (double) height/(double) width * (double) newwidth);
}
Else
{
newheight=200;
newwidth= (int) (double) width/(double) height * (double) newheight);
}
Thumimg = Upimage. Getthumbnailimage (Newwidth,newheight,callb,intptr.zero);
Outputfile=new Bitmap (upimage);
G=graphics.fromimage (outputfile);
G.drawimage (simage,new Rectangle (upimage). Width-simage.width,upimage. Height-simage.height,upimage. Width,upimage. Height), 0,0,upimage. Width,upimage. Height,graphicsunit.pixel);

String NewPath = bigpath + filename + extension; Original Diagram path
String Thumpath = smallpath + filename + extension; Thumbnail path
OutputFile. Save (NewPath);
Thumimg. Save (Thumpath);
OutputFile. Dispose ();

}
catch (Exception ex)
{
Throw ex;
}
Finally
{
if (g!=null)
G.dispose ();
if (thumimg!=null)
Thumimg. Dispose ();
if (upimage!=null)
Upimage. Dispose ();
if (simage!=null)
Simage. Dispose ();
}
}
public bool Thumbnailcallback ()
{
return false;
}



Related Article

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.