Copy Code code as follows:
<summary>
Generate JPG thumbnail byte, my small software needs to use the function, so I do a function, and you share
Why should I generate bytes instead of files, which is for the convenience of subsequent processing ^_^
</summary>
<param name= "Originalimagepath" > Original path </param>
<param name= "Quality" > Quality 0-100</param>
<param name= "width" > Breadth </param>
<param name= "height" > Height </param>
<param name= "mode" > Mode:hw,w,h,cut</param>
<returns></returns>
public static byte[] Makejpgthumbnailbytes (string originalimagepath, long quality, int width, int height, string mode)
{
Image originalimage = Image.FromFile (Originalimagepath);
MemoryStream s = new MemoryStream ();
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;
Switch (mode)
{
Case "HW"://Specify width scaling (possibly distorted)
Break
Case "W"://Specify wide, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://specified high, wide proportional
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "cut"://Specify width trim (not deformed)
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 * height/towidth;
x = 0;
y = (Originalimage.height-oh)/2;
}
Break
Default
Break
}
Create a new BMP picture
Image bitmap = new System.Drawing.Bitmap (towidth, toheight);
Create a new artboard
Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Set high quality interpolation method
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill with a transparent background color
G.clear (color.transparent);
Draws the specified portion of the original picture at the specified location and at the specified size
G.drawimage (Originalimage, New Rectangle (0, 0, Towidth, toheight),
New Rectangle (x, Y, Ow, OH),
GraphicsUnit.Pixel);
Try
{
Save thumbnails in JPG format
EncoderParameters EPS = new encoderparameters (1);
Encoderparameter EP = new Encoderparameter (encoder.quality,quality);
Eps. Param[0] = EP;
Bitmap. Save (S, getcodecinfo ("Image/jpeg"), EPS);
return S.getbuffer ();
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
S.dispose ();
G.dispose ();
}
}
/**/
<summary>
Use when saving jpg
</summary>
<param name= "MimeType" ></param>
<returns> get imagecodecinfo</returns> of designated MimeType
private static ImageCodecInfo Getcodecinfo (String mimetype)
{
imagecodecinfo[] Codecinfo = Imagecodecinfo.getimageencoders ();
foreach (ImageCodecInfo ici in codecinfo)
{
if (ICI. MimeType = = mimetype) return ici;
}
return null;
}