using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Drawing.Drawing2D;
Namespace Pub.class {
<summary>
Thumbnail processing class
</summary>
public class Cutimage
{
#region Constructors
Public Cutimage () {}
#endregion
#region Private Members
private int width;
private int height;
private int size;
private string errmsg;
#endregion
#region Public Properties
<summary>
Picture width
</summary>
public int Width {get {#;} set {width = value}}
<summary>
Picture height
</summary>
public int Height {get {return height;} set {height = value;}}
<summary>
Picture size
</summary>
public int Size {get {return size;} set {size = value;}}
<summary>
Error message
</summary>
public string ErrMsg {get {this.errmsg;} set {this.errmsg = value;}}
#endregion
#region Public methods
<summary>
Get picture information
</summary>
<param name= "ImagePath" > Picture address </param>
<returns> Success True Failed false</returns>
public bool GetImage (string ImagePath) {
try {
System.Drawing.Image image = System.drawing.image.fromfile (ImagePath);
This.width = Image.width;
This.height = Image.height;
Image.dispose ();
FileStream fs = new FileStream (ImagePath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
catch (Exception ex) {
This.errmsg = Ex.tostring ();
return false;
}
}
<summary>
Cut the chart by the width x long ratio
</summary>
<param name= "ImagePath" > Source map Address </param>
<param name= "Savepath" > New map Address </param>
<param name= "Cutwidth" > Width </param>
<param name= "Cutheight" > Height </param>
<returns> Success True Failed false</returns>
public bool Cutimagecustommin (string ImagePath, string savepath, int cutwidth, int cutheight) {
try {
System.Drawing.Image objimage = System.drawing.image.fromfile (ImagePath);
float x = objimage.width;
Float y = objimage.height;
Float xpercent = x/ Cutwidth;
Float ypercent = y/ Cutheight;
if (Xpercent < ypercent) {
this.width = (int) ((x * cutheight)/y);
this.height = cutheight;
} else {
This.width = Cutwidth;
this.height = (int) ((cutwidth * y)/x);
}
System.Drawing.Image newimage = new Bitmap (objimage.width, Objimage.height, Pixelformat.format32bpprgb);
Graphics g = Graphics.fromimage (NewImage);
G.drawimage ( Objimage, 0, 0, objimage.width, objimage.height);
g.dispose ();
System.Drawing.Image thumbimage = Newimage.getthumbnailimage (this.width, this.height, NULL, IntPtr.Zero);
Thumbimage.save ( Savepath, Objimage.rawformat);
Objimage.dispose ();
Newimage.dispose ();
Thumbimage.dispose ();
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
catch (Exception ex) {
This.errmsg = Ex.tostring ();
return false;
}
}
<summary>
Cut the chart by the width x long ratio
</summary>
<param name= "ImagePath" > Source map Address </param>
<param name= "Savepath" > New map Address </param>
<param name= "Cutwidth" > Width </param>
<param name= "Cutheight" > Height </param>
<returns> Success True Failed false</returns>
public bool Cutimagecustom (string ImagePath, string savepath, int cutwidth, int cutheight) {
try {
System.Drawing.Image objimage = System.drawing.image.fromfile (ImagePath);
float x = objimage.width;
Float y = objimage.height;
float xpercent = x/cutwidth;
float ypercent = y/cutheight;
if (Xpercent < ypercent) {
this.width = (int) ((x * cutheight)/y);
this.height = cutheight;
} else {
This.width = Cutwidth;
this.height = (int) ((cutwidth * y)/x);
}
Bitmap newimage = new Bitmap (this.width, This.height, Pixelformat.format32bpprgb);
Newimage.setresolution (72f, 72f);
Graphics gdiobj = Graphics.fromimage (newimage);
gdiobj.compositingquality = compositingquality.highquality;
Gdiobj.smoothingmode = smoothingmode.highquality;
Gdiobj.interpolationmode = Interpolationmode.highqualitybicubic;
Gdiobj.pixeloffsetmode = pixeloffsetmode.highquality;
Gdiobj.fillrectangle (New SolidBrush (Color.White), 0, 0, this.width, this.height);
Rectangle destrect = new Rectangle (0, 0, this.width, this.height);
Gdiobj.drawimage (objimage, destrect, 0, 0, objimage.width, Objimage.height, GraphicsUnit.Pixel);
Gdiobj.dispose ();
System.drawing.imaging.encoderparameters EP = new System.drawing.imaging.encoderparameters (1);
Ep.param[0] = new System.drawing.imaging.encoderparameter (system.drawing.imaging.encoder.quality, (long) 100);
imagecodecinfo[] codecs = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs) {
if (Codec.mimetype = = "Image/jpeg") {ici = codec;}
}
if (ICI!= null) newimage.save (Savepath, ICI, EP); else Newimage.save (Savepath, Objimage.rawformat);
Objimage.dispose ();
Newimage.dispose ();
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
} catch (Exception ex) {
this.errmsg = ex.tostring ();
return false;
}
}
///<summary>
/ Zooms the picture to the specified width
///</summary>
///<param name= "ImagePath" > Source map address </param>
///<param name= "Savepath" > New map Address </param>
///< Param name= "Square" > Width </param>
///<returns> Success true failed FALSE</RETURNS>
public bool Cutimagebywidth (string ImagePath, string savepath, int square) {
try {
int cutwidth = Square;
System.Drawing.Image objimage = System.drawing.image.fromfile (ImagePath);
float x = objimage.width;
Float y = objimage.height;
This.width = Cutwidth;
This.height = (int) ((cutwidth * y)/x);
System.Drawing.Image newimage = new Bitmap (objimage.width, Objimage.height, Pixelformat.format32bpprgb);
Graphics g = Graphics.fromimage (NewImage);
G.drawimage ( Objimage, 0, 0, objimage.width, objimage.height);
g.dispose ();
System.Drawing.Image thumbimage = Newimage.getthumbnailimage (this.width, this.height, NULL, IntPtr.Zero);
Thumbimage.save ( Savepath, Objimage.rawformat);
Objimage.dispose ();
Newimage.dispose ();
Thumbimage.dispose ();
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
} catch (Exception ex) {
this.errmsg = ex.tostring ();
return false;
}
}
///<summary>
/ Zooms the picture to the specified height
///</summary>
///<param name= "ImagePath" > Source map address </param>
///<param name= "Savepath" > New map Address </param>
///< Param name= "Square" > Height </param>
///<returns> Success true failed FALSE</RETURNS>
public bool Cutimagebyheight (string ImagePath, string savepath, int square) {
try {
int cutheight = Square;
System.Drawing.Image objimage = System.drawing.image.fromfile (ImagePath);
float x = objimage.width;
Float y = objimage.height;
This.height = Cutheight;
This.width = (int) ((cutheight * x)/y);
System.Drawing.Image newimage = new Bitmap (objimage.width, Objimage.height, Pixelformat.format32bpprgb);
Graphics g = Graphics.fromimage (NewImage);
G.drawimage ( Objimage, 0, 0, objimage.width, objimage.height);
g.dispose ();
System.Drawing.Image thumbimage = Newimage.getthumbnailimage (this.width, this.height, NULL, IntPtr.Zero);
Thumbimage.save ( Savepath, Objimage.rawformat);
Objimage.dispose ();
Newimage.dispose ();
Thumbimage.dispose ();
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
catch (Exception ex) {
This.errmsg = Ex.tostring ();
return false;
}
}
<summary>
Cut a picture to a square
</summary>
<param name= "ImagePath" > Source map Address </param>
<param name= "Savepath" > New map Address </param>
<param name= "Square" > Square edge length </param>
<returns> Success True Failed false</returns>
public bool Cutimagesquare (string ImagePath, string savepath, int square) {
try {
this.width = Square;
this.height = Square;
int cutwidth = Square;
int cutheight = Square;
System.Drawing.Image objimage = System.drawing.image.fromfile (ImagePath);
if (objimage.width >= objimage.height) {
cutwidth = objimage.height;
cutheight = objimage.height;
} else {
Cutwidth = Objimage.width;
cutheight = objimage.width;
}
System.Drawing.Image newimage = new Bitmap (cutwidth, Cutheight, Pixelformat.format32bpprgb);
Graphics g = Graphics.fromimage (NewImage);
G.interpolationmode = System.drawing.drawing2d.interpolationmode.high;
G.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Rectangle Destrect = new Rectangle (0, 0, cutwidth, cutheight);
Rectangle Srcrect = new Rectangle (0, 0, cutwidth, cutheight);
GraphicsUnit units = GraphicsUnit.Pixel;
G.drawimage (Objimage, Destrect, srcrect, units);
G.dispose ();
System.Drawing.Image thumbimage = Newimage.getthumbnailimage (this.width, this.height, NULL, IntPtr.Zero);
Thumbimage.save (Savepath, Objimage.rawformat);
Objimage.dispose ();
Newimage.dispose ();
Thumbimage.dispose ();
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Read);
this.size = (int) fs.length;
Fs.close ();
return true;
catch (Exception ex) {
This.errmsg = Ex.tostring ();
return false;
}
}
#region Handle picture long width display
public static void Getpropersize (int truewidth, int trueheight, int placewidth, int placeheight, out int showwidth, out in T Showheight {
if (Trueheight < Placeheight && Truewidth < placewidth) {
showheight = trueheight;
showwidth = Truewidth;
} else {
float x = (float) truewidth;
Float y = (float) Trueheight;
float xpercent = x/placewidth;
float ypercent = y/placeheight;
if (Xpercent < ypercent) {