C # clear image scaling solution [reprint]

Source: Internet
Author: User

C # clear image scaling solution _. Net tutorial Network
Http://www.soaspx.com/dotnet/csharp/csharp_20091118_1677.html

 

Hope this articleArticleIt can help friends who need to generate thumbnails frequently!

At present, the Internet has more than 4000 types of products, with more than 20 million products on sale, of which the image volume has been 8 GB so far.
Currently, our solution is to use a separate file server to store product images and access them through file.365goba.com.
On the file server, a WCF Service for uploading images is used to scale down and save uploaded images.

Reduction in the early stage of the Internet shoppingAlgorithmIt uses a tri-linear interpolation algorithm widely spread over the Internet (the effect is not very good ),CodeAs follows:

Using system;
Using system. drawing;
Using system. IO;
Using system. Drawing. imaging;
Using system. Drawing. drawing2d;

Namespace ants. Tools
{

Public class image
{

Public int width {Get; set ;}

Public int height {Get; set ;}

Private image (){}
Public Image (INT width, int height)
{
This. width = width;
This. Height = height;
}

Public memorystream gethightthumb (Stream imgdata, string mode_hw_w_h_cut)
{
Memorystream result = new memorystream ();
System. Drawing. image. getthumbnailimageabort mycallback = new system. Drawing. image. getthumbnailimageabort (thumbnailcallback );
Try
{
System. Drawing. Image originalimage = system. Drawing. image. fromstream (imgdata );

Int X, Y;
X = width;
Y = height;

Int towidth = X;
Int toheight = y;

Int x = 0;
Int y = 0;
Int ow = originalimage. width;
Int Oh = originalimage. height;

Switch (mode_hw_w_h_cut)
{
Case "HW": // specify the height/width Scaling (may be deformed) break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalimage. Height * x/originalimage. width;
Break;
Case "H // specify the height. The width is proportional to towidth = originalimage. Width * Y/originalimage. height;
Break;
Case "cut ":
If (double) originalimage. width/(double) originalimage. Height> (double) towidth/(double) toheight)
{
Oh = originalimage. height;
Ow = originalimage. Height * towidth/toheight;
Y = 0;
X = (originalimage. Width-ow)/2;
}
Else
{
Ow = originalimage. width;
Oh = originalimage. Width * Y/towidth;
X = 0;
Y = (originalimage. Height-OH)/2;
}
Break;
Default:
Break;
}

System. Drawing. Image bitmap = new system. Drawing. Bitmap (towidth, toheight );

System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (Bitmap );


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

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

G. Clear (system. Drawing. color. Transparent );

G. drawimage (originalimage, new system. Drawing. rectangle (0, 0, towidth, toheight ),
New system. Drawing. rectangle (X, Y, ow, oh ),
System. Drawing. graphicsunit. pixel );
Bitmap. Save (result, system. Drawing. imaging. imageformat. JPEG );

}
Catch
{
// Do something
}
Return result;
}}
}

 
This algorithm can meet the needs of General websites, but as an e-commerce website, the definition of product images directly affects the desire to purchase this product.
 
In order to find a better solution, we finally found a good component:Magicknet is written in C ++, but there is an available C # Calling version on the network.
Later, I will also provide this DLL file. It is worth mentioning that this component is open-source. You can study it.
MagicknetThere are a lot of functions. Here I will post the usage of his scaling method (MagicknetIt is difficult to obtain the information on the Internet)
 Using System; Using System. Collections. Generic; Using System. text; Namespace Kechendll { Public   Class Uploadfile { Private   String _ Path; // address of the image to be processed (source image) Public Uploadfile ( String Path ){This . _ Path = path ;} Private Uploadfile (){} Public   Void Resize ( Int Width, Int Height, String Savetopath) {magicknet. Image IMG = New Magicknet. Image (_ path); IMG. Quality = 100; system. Drawing. Bitmap bitmap = New System. Drawing. Bitmap (_ path ); Int X = bitmap. width; Int Y = bitmap. height; Float Rank = ( Float ) X/y; If (X> Y) {Height = convert. toint32 (height/RANK );} Else {Width = convert. toint32 (width * Rank);} IMG. Resize ( New System. Drawing. Size (width, height); IMG. Write (savetopath); IMG. Dispose ();}}}

 

the original text only represents my opinion or conclusion at a certain time. I do not guarantee any code involved. Reprinted, please indicate the source!

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.