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.