C # image interception compression (percent compression/size compression) Implementation code _ Practical skills

Source: Internet
Author: User
Tags abs
Front-end time friends to pass some pictures to me, is full of big picture, considering the speed limit, let him deal with the picture size again to me, this fellow incredibly do not know what tool.

For entertainment, I've written an intercept picture and a compressed picture, your gadget.
1. Screenshot by percentage
Copy Code code as follows:

View Code
<summary>
Shrink pictures by scale
</summary>
<param name= "Srcimage" > Want to shrink the picture </param>
<param name= "percent" > reduction ratio </param>
<returns> results after narrowing </returns>
public static Bitmap Percentimage (Image srcimage, double percent)
{
The height after shrinking
int newh = Int. Parse (Math.Round (Srcimage.height * percent). ToString ());
The width after narrowing
int neww = Int. Parse (Math.Round (Srcimage.width * percent). ToString ());
Try
{
Picture to save to
Bitmap B = new Bitmap (NEWW, NEWH);
Graphics g = graphics.fromimage (b);
The quality of interpolation algorithm
G.interpolationmode = Interpolationmode.default;
G.drawimage (Srcimage, New Rectangle (0, 0, NEWW, NEWH), new Rectangle (0, 0, Srcimage.width, srcimage.height), GraphicsUnit . Pixel);
G.dispose ();
return b;
}
catch (Exception)
{
return null;
}
}

2. Screenshot by specified pixel size
Copy Code code as follows:

View Code
<summary>
Scale pictures by the specified size
</summary>
<param name= "Srcimage" ></param>
<param name= "Iwidth" ></param>
<param name= "Iheight" ></param>
<returns></returns>
public static Bitmap Sizeimage (Image srcimage, int iwidth, int iheight)
{
Try
{
Picture to save to
Bitmap B = new Bitmap (iwidth, iheight);
Graphics g = graphics.fromimage (b);
The quality of interpolation algorithm
G.interpolationmode = Interpolationmode.highqualitybicubic;
G.drawimage (Srcimage, New Rectangle (0, 0, iwidth, iheight), new Rectangle (0, 0, Srcimage.width, srcimage.height), Graphic Sunit.pixel);
G.dispose ();
return b;
}
catch (Exception)
{
return null;
}
}

3. According to the specified pixel size screenshot (but in order to ensure the original proportions of the picture, the picture will be intercepted from the center, to achieve the picture is not stretched effect)
Copy Code code as follows:

View Code
<summary>
Scales the picture according to the specified size, but to ensure that the picture is automatically truncated to the aspect ratio
</summary>
<param name= "Srcimage" ></param>
<param name= "Iwidth" ></param>
<param name= "Iheight" ></param>
<returns></returns>
public static Bitmap sizeimagewitholdpercent (Image srcimage, int iwidth, int iheight)
{
Try
{
To intercept the width of a picture (temporary picture)
int neww = Srcimage.width;
To intercept the height of a picture (temporary picture)
int NEWH = Srcimage.height;
Intercept start horizontal (temporary picture)
int newx = 0;
Intercept start ordinate (temporary picture)
int newy = 0;
Interception ratio (temporary picture)
Double whpercent = 1;
Whpercent = ((double) iwidth/(double) iheight) * (double) srcimage.height/(double) srcimage.width);
if (Whpercent > 1)
{
The current picture width is too large for the percentage to intercept
NEWW = Int. Parse (Math.Round (srcimage.width/whpercent). ToString ());
}
else if (Whpercent < 1)
{
The height of the current picture is too large for the percentage to intercept
NEWH = Int. Parse (Math.Round (Srcimage.height * whpercent). ToString ());
}
if (neww!= srcimage.width)
{
Adjust the horizontal axis of the intercept when the width is changed
NEWX = math.abs (int. Parse (Math.Round ((double) srcimage.width-neww)/2). ToString ()));
}
else if (NEWH = = srcimage.height)
{
When height changes, adjust the ordinate that begins to intercept
Newy = math.abs (int. Parse (Math.Round (double) srcimage.height-(double) NEWH)/2). ToString ()));
}
Obtaining a proportionate temporary document
Bitmap cutedimage = Cutimage (Srcimage, Newx, Newy, NEWW, NEWH);
Files saved to
Bitmap B = new Bitmap (iwidth, iheight);
Graphics g = graphics.fromimage (b);
The quality of interpolation algorithm
G.interpolationmode = Interpolationmode.default;
G.drawimage (Cutedimage, New Rectangle (0, 0, iwidth, iheight), new Rectangle (0, 0, Cutedimage.width, cutedimage.height), G Raphicsunit.pixel);
G.dispose ();
return b;
}
catch (Exception)
{
return null;
}
}

4.jpeg image quality compression, compression of the proportional parameters between 1-100. (The right amount of compression for the naked eye there is no obvious difference, but can greatly reduce the size of the picture)
Copy Code code as follows:

View Code
<summary>
JPEG image compression
</summary>
<param name= "Sfile" ></param>
<param name= "Outpath" ></param>
<param name= "Flag" ></param>
<returns></returns>
public static bool Getpicthumbnail (String sfile, string outpath, int flag)
{
System.Drawing.Image ISource = System.Drawing.Image.FromFile (sfile);
ImageFormat Tformat = Isource.rawformat;
The following code sets the compression quality when saving a picture
EncoderParameters EP = new EncoderParameters ();
long[] qy = new Long[1];
Qy[0] = flag;//Set the ratio of compression to 1-100
Encoderparameter Eparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, QY);
Ep. Param[0] = Eparam;
Try
{
imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();
ImageCodecInfo jpegiciinfo = null;
for (int x = 0; x < Arrayici.length + +)
{
if (arrayici[x). Formatdescription.equals ("JPEG"))
{
Jpegiciinfo = Arrayici[x];
Break
}
}
if (jpegiciinfo!= null)
{
Isource.save (Outpath, Jpegiciinfo, EP);//dfile is a new compressed path
}
Else
{
Isource.save (Outpath, Tformat);
}
return true;
}
Catch
{
return false;
}
Finally
{
Isource.dispose ();
Isource.dispose ();
}
}

PS: Supplement of the Cutimage method used above
Copy Code code as follows:

View Code
<summary>
Tailoring--with GDI +
</summary>
<param name= "B" > Original bitmap</param>
<param name= "StartX" > Start coordinates x</param>
<param name= "Starty" > Start coordinates y</param>
<param name= "iwidth" > Width </param>
<param name= "iheight" > Height </param>
<returns> the bitmap</returns> after tailoring
public static Bitmap Cutimage (Image b, int startx, int starty, int iwidth, int iheight)
{
if (b = = null)
{
return null;
}
int w = b.width;
int h = b.height;
if (startx >= W | | Starty >= h)
{
End processing when intercept coordinates are too large
return null;
}
if (StartX + iwidth > W)
{
Only intercept to maximum size when the width is too large
iwidth = W-startx;
}
if (Starty + iheight > H)
{
Only the maximum size is intercepted when the height is too large
Iheight = H-starty;
}
Try
{
Bitmap bmpout = new Bitmap (iwidth, iheight);
Graphics g = graphics.fromimage (bmpout);
G.drawimage (b, New Rectangle (0, 0, iwidth, iheight), New Rectangle (StartX, Starty, iwidth, iheight), GraphicsUnit.Pixel);
G.dispose ();
return bmpout;
}
Catch
{
return null;
}
}

Record the intercepted code again, albeit simply, if it takes time to rewrite it.

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.