C # picture Compression processing

Source: Internet
Author: User

Recently in the photo album feature encountered a problem, is to load the picture and then zoom in, feel a little card. The picture may be a bit large, so consider using thumbnails to implement it.

That is, in the query out of an album picture, a page showing dozens of, the dozens of pictures with thumbnails to show, see believe that when the QQ photo album sample loading images to enlarge pull pictures related information. The technology used is the image compression function. Now the functionality has been implemented to see the code.

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Drawing.Imaging;

Using System.IO;
            namespace WindowsFormsApplication1 {public partial class Form1:form {public Form1 () {
        InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {string pathperc = @ "D:\0046620140
            807150650cin.jpg ";
            String Source = @ "G:\0046620140807150650cin.jpg"; if (! File.exists (PATHPERC)) {file.create (PATHPERC).
            Close ();
                else {file.delete (PATHPERC); File.create (PATHPERC).
            Close ();
        Getthumimage (source, 3, PATHPERC); #region getthumimage/**////<summary>///generate thumbnails///</summary>///<param name= "SourceFile" > Original picture file </param>///<param name= "Quality" > Quality Volume compression ratio </param>///<param name= "multiple" > Contraction multiples </param>///<param name= "OutputFile" > Output filename </param>///<returns> Successful return True, Failure returns false</returns> public static bool Getthumimage
        (String sourcefile, long quality, int multiple, string outputfile)
                {try {long imagequality = quality;
                Bitmap sourceimage = new Bitmap (sourcefile);
                ImageCodecInfo myimagecodecinfo = Getencoderinfo ("Image/jpeg");
                System.Drawing.Imaging.Encoder myencoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myencoderparameters = new EncoderParameters (1);
                Encoderparameter myencoderparameter = new Encoderparameter (Myencoder, imagequality); Myencoderparameters.param[0] = MyencoderparAmeter;
                float xwidth = sourceimage.width;
                float ywidth = sourceimage.height;
                Bitmap newimage = new Bitmap (int) (xwidth/multiple), (int) (ywidth/multiple));

                Graphics g = graphics.fromimage (newimage);
                G.drawimage (sourceimage, 0, 0, xwidth/multiple, ywidth/multiple);
                G.dispose ();
                Newimage.save (OutputFile, Myimagecodecinfo, myencoderparameters);
            return true;
            catch {return false; #endregion getthumimage/**////<summary>///get picture encoding information///
            ;/summary> private static ImageCodecInfo Getencoderinfo (String mimetype) {int J;
            Imagecodecinfo[] encoders;
            Encoders = Imagecodecinfo.getimageencoders (); for (j = 0; J < encoders. Length; ++J) {if (ENCODERS[J].
            MimeType = = mimetype) return encoders[j];
        return null;
 }


    }
}



Look at the compressed image below


11KB

Pictures before compression


So the thumbnail succeeds.





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.