asp.net large image generation small picture generation class

Source: Internet
Author: User
Tags int size

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


&lt;summary&gt;


Picture width


&lt;/summary&gt;


public int Width {get {#;} set {width = value}}


&lt;summary&gt;


Picture height


&lt;/summary&gt;


public int Height {get {return height;} set {height = value;}}


&lt;summary&gt;


Picture size


&lt;/summary&gt;


public int Size {get {return size;} set {size = value;}}


&lt;summary&gt;


Error message


&lt;/summary&gt;


public string ErrMsg {get {this.errmsg;} set {this.errmsg = value;}}


#endregion

#region Public methods


&lt;summary&gt;


Get picture information


&lt;/summary&gt;


&lt;param name= "ImagePath" &gt; Picture address &lt;/param&gt;


&lt;returns&gt; Success True Failed false&lt;/returns&gt;


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;
}
}

&lt;summary&gt;


Cut the chart by the width x long ratio


&lt;/summary&gt;


&lt;param name= "ImagePath" &gt; Source map Address &lt;/param&gt;


&lt;param name= "Savepath" &gt; New map Address &lt;/param&gt;


&lt;param name= "Cutwidth" &gt; Width &lt;/param&gt;


&lt;param name= "Cutheight" &gt; Height &lt;/param&gt;


&lt;returns&gt; Success True Failed false&lt;/returns&gt;


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;
}
}

&lt;summary&gt;


Cut the chart by the width x long ratio


&lt;/summary&gt;


&lt;param name= "ImagePath" &gt; Source map Address &lt;/param&gt;


&lt;param name= "Savepath" &gt; New map Address &lt;/param&gt;


&lt;param name= "Cutwidth" &gt; Width &lt;/param&gt;


&lt;param name= "Cutheight" &gt; Height &lt;/param&gt;


&lt;returns&gt; Success True Failed false&lt;/returns&gt;


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;
}
}

&lt;summary&gt;


Cut a picture to a square


&lt;/summary&gt;


&lt;param name= "ImagePath" &gt; Source map Address &lt;/param&gt;


&lt;param name= "Savepath" &gt; New map Address &lt;/param&gt;


&lt;param name= "Square" &gt; Square edge length &lt;/param&gt;


&lt;returns&gt; Success True Failed false&lt;/returns&gt;


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) {

Related Article

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.