asp.net 大图生成小图图片生成类

作者:袖梨 2022-06-25

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


    /// 缩图处理类
    ///

    public class cutimage
    {
        #region 构造函数
        public cutimage() { }
        #endregion

        #region 私有成员
        private int width;
        private int height;
        private int size;
        private string errmsg;
        #endregion

        #region 公共属性
        ///


        /// 图片宽度
        ///

        public int width { get { return width; } set { width = value; } }
        ///
        /// 图片高度
        ///

        public int height { get { return height; } set { height = value; } }
        ///
        /// 图片尺寸
        ///

        public int size { get { return size; } set { size = value; } }
        ///
        /// 错误消息
        ///

        public string errmsg { get { return this.errmsg; } set { this.errmsg = value; } }
        #endregion

        #region 公共方法
        ///


        /// 获取图片信息
        ///

        /// 图片地址
        /// 成功true失败false
        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;
            }
        }

        ///


        /// 按宽x长比例切图
        ///

        /// 源图地址
        /// 新图地址
        /// 宽度
        /// 高度
        /// 成功true失败false
        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;
            }
        }

        ///


        /// 按宽x长比例切图
        ///

        /// 源图地址
        /// 新图地址
        /// 宽度
        /// 高度
        /// 成功true失败false
        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;
            }
        }

        ///


        /// 将图片缩放到指定的宽度
        ///

        /// 源图地址
        /// 新图地址
        /// 宽度
        /// 成功true失败false
        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;
            }
        }

        ///


        /// 将图片缩放到指定的高度
        ///

        /// 源图地址
        /// 新图地址
        /// 高度
        /// 成功true失败false
        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;
            }
        }

        ///


        /// 将图片剪切到一个正方形
        ///

        /// 源图地址
        /// 新图地址
        /// 正方形边长
        /// 成功true失败false
        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 处理图片长宽显示
        public static void getpropersize(int truewidth, int trueheight, int placewidth, int placeheight, out int showwidth, out int 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) {
         

相关文章

精彩推荐