PS image uploads images to generate a thumbnail at the same time

Source: Internet
Author: User

I. Technical Overview:

1. Ajax does not need to upload images. For more information, see my article (jquery + C # ashx)

2. C # Bitmap Processing System. Drawing.

3. The latest demo supports IE7, IE8, and Firefox.

Ii. How to Deal with thumbnail:

Core Method for generating a thumbnail:

Createthumbnailpicture

///

/// Image thumbnail Processing

///

///

///

///

///

Public static void createthumbnailpicture (string srcpath, string destpath, int width, int height)

{

// Obtain the image object of the source image based on the absolute path of the image Disk

System. Drawing. Image IMG = system. Drawing. image. fromfile (srcpath );

// BMP: the thumbnail bitmap object to be created. bitmap BMP = new Bitmap (width, height );

// G: draws a BMP graphics object.

Graphics G = graphics. fromimage (BMP );

G. Clear (color. Transparent );

// Initialize necessary parameters for the graphics g object, which is easy to understand.

G. pixeloffsetmode = system. Drawing. drawing2d. pixeloffsetmode. highquality;

G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;

G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;

G. compositingquality = system. Drawing. drawing2d. compositingquality. highquality;

// Source image width and height

Int imgwidth = IMG. width;

Int imgheight = IMG. height;

// Draw a thumbnail

G. drawimage (IMG, new system. drawing. rectangle (0, 0, width, height), new system. drawing. rectangle (0, 0, imgwidth, imgheight), graphicsunit. pixel );

Imageformat format = IMG. rawformat;

Imagecodecinfo info = imagecodecinfo. getimageencoders (). singleordefault (I => I. formatid = format. guid );

Encoderparameter Param = new encoderparameter (system. Drawing. imaging. encoder. Quality, 100l );

Encoderparameters parameters = new encoderparameters (1 );

Parameters. Param [0] = Param;

IMG. Dispose ();

// Save the generated thumbnail. Here, the GIF format is converted to PNG format.

If (format = imageformat. GIF)

{

Destpath = destpath. tolower (). Replace (". GIF", ". PNG ");

BMP. Save (destpath, imageformat. PNG );

}

Else

{

If (info! = NULL)

{

BMP. Save (destpath, info, parameters );

}

Else

{

BMP. Save (destpath, format );

}

}

IMG. Dispose ();

G. Dispose ();

BMP. Dispose ();

}

Some codes have been added with comments. It is hard to understand the codes carefully. the following describes the Ajax call method in ashx. After Ajax asynchronously uploads an image, we perform "Ps" on the source image ". the key code snippets are as follows:

// The relative path of the website's endogenous images after the upload is successful

String relativepath = system. Web. httpcontext. Current. Request. applicationpath

+ String. Format (@ "content/upload/images/{0}", filename );

/*

Proportional Processing

Scale down the Image Height (the defaultheight attribute value is 400)

*/

System. Drawing. Image IMG = system. Drawing. image. fromfile (tofile );

Int width = IMG. width;

Int Height = IMG. height;

Float ratio = (float) width/height;

// Thumbnail height and width

Int newheight = height <= defaultheight? Height: defaultheight;

Int newwidth = height <= defaultheight? Width: Convert. toint32 (defaultheight * ratio );

Fileinfo generatedfile = new fileinfo (tofile );

String newfilename = "Thumb _" + generatedfile. Name;

String newfilepath = path. Combine (generatedfile. directoryname, newfilename );

Picturehandler. createthumbnailpicture (tofile, newfilepath, newwidth, newheight );

String thumbrelativepath = system. Web. httpcontext. Current. Request. applicationpath

+ String. Format (@ "/content/upload/images/{0}", newfilename );

// Returns the relative path of the original image and thumbnail.

Relativepath = string. Format ("{0}, {1}", relativepath, thumbrelativepath );

Return relativepath;

3. program running:

Before uploading:

After uploading:

Iv. Summary:

This method is mainly used to solve the report style deformation caused by the lack of reasonable proportion specifications in the image size during report printing, this method is also suitable for websites or forums where users upload source images to generate thumbnail portraits.

Finally, I hope this article will help you solve similar problems in development.

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.