Restrict image size generation and other proportions of thumbnails asp.net and js Code

Source: Internet
Author: User
Tags bmp image

/// Description: A loading flag is displayed before the image is loaded. After the image is downloaded, the image is displayed.
// Whether the image can be scaled automatically
// This plug-in allows the page to be loaded first, and the image to be loaded later,
// Solve the problem that the layout can be scaled only when the image is displayed during normal use.
/// Parameter settings:
// Whether or not scaling scales proportionally
// Width: Maximum Image Height
// Height maximum Image Width
// Path of the image loaded by loadpic
JQuery. fn. LoadImage = function (scaling, width, height, loadpic ){
If (loadpic = null) loadpic = "load3.gif ";
Return this. each (function (){
Var t = $ (this );
Var src = $ (this). attr ("src ")
Var img = new Image ();
// Alert ("Loading ")
Img. src = src;
// Auto Scaling
Var autoScaling = function (){
If (scaling ){
If (img. width> 0 & img. height> 0 ){
If (img. width/img. height> = width/height ){
If (img. width> width ){
T. width (width );
T. height (img. height * width)/img. width );
} Else {
T. width (img. width );
T. height (img. height );
}
}
Else {
If (img. height> height ){
T. height (height );
T. width (img. width * height)/img. height );
} Else {
T. width (img. width );
T. height (img. height );
}
}
}
}
}
// The cached image is automatically read under ff.
If (img. complete ){
// Alert ("getToCache! ");
AutoScaling ();
Return;
}
$ (This). attr ("src ","");
Var loading = $ (" ");
T. hide ();
T. after (loading );
$ (Img). load (function (){
AutoScaling ();
Loading. remove ();
T. attr ("src", this. src );
Loading. remove ();
T. show ();
// Alert ("finally! ")
});
});
}

$ ("Li img"). LoadImage (true, 120,120 );

 

 

 

Asp.net code generation and other proportional thumbnails with High Quality

/// <Summary>
/// Generate proportional high-definition thumbnail
/// </Summary>
/// <Param name = "sourcePath"> path of the image to be generated (the virtual path uploaded to the server) </param>
/// <Param name = "newPath"> path of the generated Thumbnail (Virtual Path) </param>
/// <Param name = "width"> width of the thumbnail to be generated </param>
/// <Param name = "height"> height of the thumbnail to be generated </param>
/// <Returns> whether the returned result is successful </returns>
Public static bool CreateMiniature (string sourcePath, string newPath, int width, int height)
{
Bool result = false;
Double w = 0.0;
Double h = 0.0;
Double mw = Convert. ToDouble (width );
Double mh = Convert. ToDouble (height );
String sourcePathPhysical = System. Web. HttpContext. Current. Server. MapPath (sourcePath );
String newPathPhysical = System. Web. HttpContext. Current. Server. MapPath (newPath );
Try
{
System. IO. FileInfo myFile = new System. IO. FileInfo (sourcePathPhysical );

If (myFile. Exists)
{
// Obtain an image object from a file
System. Drawing. Image image = System. Drawing. Image. FromFile (sourcePathPhysical );
Double sw = Convert. ToDouble (image. Width );
Double sh = Convert. ToDouble (image. Height );

If (sw <mw & sh <mh)
{
W = sw;
H = sh;
}
Else if (sw/sh)> (mw/mh ))
{
W = width;
H = (w * sh)/sw;
}
Else
{
H = height;
W = (h * sw)/sh;
}

// Obtain the image size
System. Drawing. Size size = new Size (Convert. ToInt32 (w), Convert. ToInt32 (h ));
// Create a bmp Image
System. Drawing. Image bitmap = new System. Drawing. Bitmap (size. Width, size. Height );
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap );
// Set a high quality Interpolation Method
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
// Set high quality and smooth Low Speed
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
// Clear the canvas
G. Clear (Color. Blue );
// Draw a picture at a specified position
G. DrawImage (image, new System. Drawing. Rectangle (0, 0, bitmap. Width, bitmap. Height ),
New System. Drawing. Rectangle (0, 0, image. Width, image. Height ),
System. Drawing. GraphicsUnit. Pixel );
// Save high-definition thumbnails
Bitmap. Save (newPathPhysical, System. Drawing. Imaging. ImageFormat. Jpeg );
G. Dispose ();
Image. Dispose ();
// MyFile. Delete (); // Delete the original image
Result = true;
}
}
Catch
{
Result = false;
}

Return result;
}

 

 

 

Asp.net generates a fixed-size thumbnail that is not enough to fill in white

 

 

/// <Summary>
/// Static method for generating thumbnails
/// </Summary>
/// <Param name = "pathImageFrom"> source image path (including the physical path with the file name and extension) </param>
/// <Param name = "pathImageTo"> path of the generated Thumbnail (including the physical path of the file name and extension)
/// Note: the extension must correspond to the generated thumbnail format </param>
/// <Param name = "width"> width (pixel value) of the thumbnail canvas to be generated </param>
/// <Param name = "height"> height (pixel value) of the thumbnail "canvas" to be generated </param>
Public static void GenThumbnail (string pathImageFrom, string pathImageTo, int width, int height)
{
Image imageFrom = null;

Try
{
ImageFrom = Image. FromFile (pathImageFrom );
}
Catch
{
// Throw;
}

If (imageFrom = null)
{
Return;
}

// Source image width and height
Int imageFromWidth = imageFrom. Width;
Int imageFromHeight = imageFrom. Height;

// The actual width and height of the generated thumbnail
Int bitmapWidth = width;
Int bitmapHeight = height;

// Position of the generated thumbnail on the canvas
Int X = 0;
Int Y = 0;

// Calculate the actual size of the thumbnail and its position on the canvas based on the source image and the size of the thumbnail to be generated.
If (bitmapHeight * imageFromWidth> bitmapWidth * imageFromHeight)
{
BitmapHeight = imageFromHeight * width/imageFromWidth;
Y = (height-bitmapHeight)/2;
}
Else
{
BitmapWidth = imageFromWidth * height/imageFromHeight;
X = (width-bitmapWidth)/2;
}

// Create a canvas
Bitmap bmp = new Bitmap (width, height );
Graphics g = Graphics. FromImage (bmp );

// Clear with white
G. Clear (Color. White );

// Specify a high quality double-cubic Interpolation Method. Perform pre-filtering to ensure high-quality shrinkage. This mode produces the highest quality conversion images.
G. InterpolationMode = InterpolationMode. HighQualityBicubic;

// Specify high quality and low-speed rendering.
G. SmoothingMode = SmoothingMode. HighQuality;

// Draw the specified part of the specified Image at the specified position and in the specified size.
G. DrawImage (imageFrom, new Rectangle (X, Y, bitmapWidth, bitmapHeight), new Rectangle (0, 0, imageFromWidth, imageFromHeight), GraphicsUnit. Pixel );

Try
{
// The size and quality of. jpg thumbnails are optimized.
Bmp. Save (pathImageTo, ImageFormat. Jpeg );
}
Catch
{
}
Finally
{
// Display the resources released
ImageFrom. Dispose ();
Bmp. Dispose ();
G. Dispose ();
}
}

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.