c#图片缩放图片剪切功能实现(等比缩放)

所谓c#图片处理高级应,多数是基于.net framework类库完成

代码如下:

using system;
using system.collections.generic;
using system.text;
using system.io;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;

namespace wujian.common
{
    /// <summary>
    /// 图片处理类
    /// </summary>
    public class ptimage
    {
        #region 正方型裁剪并缩放
        /// <summary>
        /// 正方型裁剪
        /// 以图片中心为轴心,截取正方型,然后等比缩放
        /// 用于头像处理
        /// </summary>
        /// <param name="postedfile">原图httppostedfile对象</param>
        /// <param name="filesaveurl">缩略图存放地址</param>
        /// <param name="side">指定的边长(正方型)</param>
        /// <param name="quality">质量(范围0-100)</param>
        public static void cutforsquare(system.web.httppostedfile postedfile, string filesaveurl, int side, int quality)
        {
            //创建目录
            string dir = path.getdirectoryname(filesaveurl);
            if (!directory.exists(dir))
                directory.createdirectory(dir);

//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
            system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);

//原图宽高均小于模版,不作处理,直接保存
            if (initimage.width <= side && initimage.height <= side)
            {
                initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
            }
            else
            {
                //原始图片的宽、高
                int initwidth = initimage.width;
                int initheight = initimage.height;

//非正方型先裁剪为正方型
                if (initwidth != initheight)
                {
                    //截图对象
                    system.drawing.image pickedimage = null;
                    system.drawing.graphics pickedg = null;

//宽大于高的横图
                    if (initwidth > initheight)
                    {
                        //对象实例化
                        pickedimage = new system.drawing.bitmap(initheight, initheight);
                        pickedg = system.drawing.graphics.fromimage(pickedimage);
                        //设置质量
                        pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                        pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                        //定位
                        rectangle fromr = new rectangle((initwidth - initheight) / 2, 0, initheight, initheight);
                        rectangle tor = new rectangle(0, 0, initheight, initheight);
                        //画图
                        pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
                        //重置宽
                        initwidth = initheight;
                    }
                    //高大于宽的竖图
                    else
                    {
                        //对象实例化
                        pickedimage = new system.drawing.bitmap(initwidth, initwidth);
                        pickedg = system.drawing.graphics.fromimage(pickedimage);
                        //设置质量
                        pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                        pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                        //定位
                        rectangle fromr = new rectangle(0, (initheight - initwidth) / 2, initwidth, initwidth);
                        rectangle tor = new rectangle(0, 0, initwidth, initwidth);
                        //画图
                        pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
                        //重置高
                        initheight = initwidth;
                    }

//将截图对象赋给原图
                    initimage = (system.drawing.image)pickedimage.clone();
                    //释放截图资源
                    pickedg.dispose();
                    pickedimage.dispose();
                }

//缩略图对象
                system.drawing.image resultimage = new system.drawing.bitmap(side, side);
                system.drawing.graphics resultg = system.drawing.graphics.fromimage(resultimage);
                //设置质量
                resultg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                resultg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                //用指定背景色清空画布
                resultg.clear(color.white);
                //绘制缩略图
                resultg.drawimage(initimage, new system.drawing.rectangle(0, 0, side, side), new system.drawing.rectangle(0, 0, initwidth, initheight), system.drawing.graphicsunit.pixel);

//关键质量控制
                //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                imagecodecinfo[] icis = imagecodecinfo.getimageencoders();
                imagecodecinfo ici = null;
                foreach (imagecodecinfo i in icis)
                {
                    if (i.mimetype == "image/jpeg" || i.mimetype == "image/bmp" || i.mimetype == "image/png" || i.mimetype == "image/gif")
                    {
                        ici = i;
                    }
                }
                encoderparameters ep = new encoderparameters(1);
                ep.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, (long)quality);

//保存缩略图
                resultimage.save(filesaveurl, ici, ep);

//释放关键质量控制所用资源
                ep.dispose();

//释放缩略图资源
                resultg.dispose();
                resultimage.dispose();

//释放原始图片资源
                initimage.dispose();
            }
        }

/// <summary>
        /// 正方型裁剪
        /// 以图片中心为轴心,截取正方型,然后等比缩放
        /// 用于头像处理
        /// </summary>
        /// <param name="postedfile">原图httppostedfile对象</param>
        /// <param name="filesaveurl">缩略图存放地址</param>
        /// <param name="side">指定的边长(正方型)</param>
        /// <param name="quality">质量(范围0-100)</param>
        public static void cutforsquare(system.io.stream fromfile, string filesaveurl, int side, int quality)
        {
            //创建目录
            string dir = path.getdirectoryname(filesaveurl);
            if (!directory.exists(dir))
                directory.createdirectory(dir);

//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
            system.drawing.image initimage = system.drawing.image.fromstream(fromfile, true);

//原图宽高均小于模版,不作处理,直接保存
            if (initimage.width <= side && initimage.height <= side)
            {
                initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
            }
            else
            {
                //原始图片的宽、高
                int initwidth = initimage.width;
                int initheight = initimage.height;

//非正方型先裁剪为正方型
                if (initwidth != initheight)
                {
                    //截图对象
                    system.drawing.image pickedimage = null;
                    system.drawing.graphics pickedg = null;

//宽大于高的横图
                    if (initwidth > initheight)
                    {
                        //对象实例化
                        pickedimage = new system.drawing.bitmap(initheight, initheight);
                        pickedg = system.drawing.graphics.fromimage(pickedimage);
                        //设置质量
                        pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                        pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                        //定位
                        rectangle fromr = new rectangle((initwidth - initheight) / 2, 0, initheight, initheight);
                        rectangle tor = new rectangle(0, 0, initheight, initheight);
                        //画图
                        pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
                        //重置宽
                        initwidth = initheight;
                    }
                    //高大于宽的竖图
                    else
                    {
                        //对象实例化
                        pickedimage = new system.drawing.bitmap(initwidth, initwidth);
                        pickedg = system.drawing.graphics.fromimage(pickedimage);
                        //设置质量
                        pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                        pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                        //定位
                        rectangle fromr = new rectangle(0, (initheight - initwidth) / 2, initwidth, initwidth);
                        rectangle tor = new rectangle(0, 0, initwidth, initwidth);
                        //画图
                        pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
                        //重置高
                        initheight = initwidth;
                    }

//将截图对象赋给原图
                    initimage = (system.drawing.image)pickedimage.clone();
                    //释放截图资源
                    pickedg.dispose();
                    pickedimage.dispose();
                }

//缩略图对象
                system.drawing.image resultimage = new system.drawing.bitmap(side, side);
                system.drawing.graphics resultg = system.drawing.graphics.fromimage(resultimage);
                //设置质量
                resultg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                resultg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                //用指定背景色清空画布
                resultg.clear(color.white);
                //绘制缩略图
                resultg.drawimage(initimage, new system.drawing.rectangle(0, 0, side, side), new system.drawing.rectangle(0, 0, initwidth, initheight), system.drawing.graphicsunit.pixel);

//关键质量控制
                //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                imagecodecinfo[] icis = imagecodecinfo.getimageencoders();
                imagecodecinfo ici = null;
                foreach (imagecodecinfo i in icis)
                {
                    if (i.mimetype == "image/jpeg" || i.mimetype == "image/bmp" || i.mimetype == "image/png" || i.mimetype == "image/gif")
                    {
                        ici = i;
                    }
                }
                encoderparameters ep = new encoderparameters(1);
                ep.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, (long)quality);

//保存缩略图
                resultimage.save(filesaveurl, ici, ep);

//释放关键质量控制所用资源
                ep.dispose();

//释放缩略图资源
                resultg.dispose();
                resultimage.dispose();

//释放原始图片资源
                initimage.dispose();
            }
        }
        #endregion

#region 固定模版裁剪并缩放
        /// <summary>
        /// 指定长宽裁剪
        /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸
        /// </summary>
        /// <param name="postedfile">原图httppostedfile对象</param>
        /// <param name="filesaveurl">保存路径</param>
        /// <param name="maxwidth">最大宽(单位:px)</param>
        /// <param name="maxheight">最大高(单位:px)</param>
        /// <param name="quality">质量(范围0-100)</param>
        public static void cutforcustom(system.web.httppostedfile postedfile, string filesaveurl, int maxwidth, int maxheight, int quality)
        {
            //从文件获取原始图片,并使用流中嵌入的颜色管理信息
            system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);

//原图宽高均小于模版,不作处理,直接保存
            if (initimage.width <= maxwidth && initimage.height <= maxheight)
            {
                initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
            }
            else
            {
                //模版的宽高比例
                double templaterate = (double)maxwidth / maxheight;
                //原图片的宽高比例
                double initrate = (double)initimage.width / initimage.height;

//原图与模版比例相等,直接缩放
                if (templaterate == initrate)
                {
                    //按模版大小生成最终图片
                    system.drawing.image templateimage = new system.drawing.bitmap(maxwidth, maxheight);
                    system.drawing.graphics templateg = system.drawing.graphics.fromimage(templateimage);
                    templateg.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
                    templateg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                    templateg.clear(color.white);
                    templateg.drawimage(initimage, new system.drawing.rectangle(0, 0, maxwidth, maxheight), new system.drawing.rectangle(0, 0, initimage.width, initimage.height), system.drawing.graphicsunit.pixel);
                    templateimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
                }
                //原图与模版比例不等,裁剪后缩放
                else
                {
                    //裁剪对象
                    system.drawing.image pickedimage = null;
                    system.drawing.graphics pickedg = null;

//定位
                    rectangle fromr = new rectangle(0, 0, 0, 0);//原图裁剪定位
                    rectangle tor = new rectangle(0, 0, 0, 0);//目标定位

//宽为标准进行裁剪
                    if (templaterate > initrate)
                    {
                        //裁剪对象实例化
                        pickedimage = new system.drawing.bitmap(initimage.width, (int)math.floor(initimage.width / templaterate));
                        pickedg = system.drawing.graphics.fromimage(pickedimage);

//裁剪源定位
                        fromr.x = 0;
                        fromr.y = (int)math.floor((initimage.height - initimage.width / templaterate) / 2);
                        fromr.width = initimage.width;
                        fromr.height = (int)math.floor(initimage.width / templaterate);

//裁剪目标定位
                        tor.x = 0;
                        tor.y = 0;
                        tor.width = initimage.width;
                        tor.height = (int)math.floor(initimage.width / templaterate);
                    }
                    //高为标准进行裁剪
                    else
                    {
                        pickedimage = new system.drawing.bitmap((int)math.floor(initimage.height * templaterate), initimage.height);
                        pickedg = system.drawing.graphics.fromimage(pickedimage);

fromr.x = (int)math.floor((initimage.width - initimage.height * templaterate) / 2);
                        fromr.y = 0;
                        fromr.width = (int)math.floor(initimage.height * templaterate);
                        fromr.height = initimage.height;

tor.x = 0;
                        tor.y = 0;
                        tor.width = (int)math.floor(initimage.height * templaterate);
                        tor.height = initimage.height;
                    }

//设置质量
                    pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                    pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;

//裁剪
                    pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);

//按模版大小生成最终图片
                    system.drawing.image templateimage = new system.drawing.bitmap(maxwidth, maxheight);
                    system.drawing.graphics templateg = system.drawing.graphics.fromimage(templateimage);
                    templateg.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
                    templateg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
                    templateg.clear(color.white);
                    templateg.drawimage(pickedimage, new system.drawing.rectangle(0, 0, maxwidth, maxheight), new system.drawing.rectangle(0, 0, pickedimage.width, pickedimage.height), system.drawing.graphicsunit.pixel);

//关键质量控制
                    //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                    imagecodecinfo[] icis = imagecodecinfo.getimageencoders();
                    imagecodecinfo ici = null;
                    foreach (imagecodecinfo i in icis)
                    {
                        if (i.mimetype == "image/jpeg" || i.mimetype == "image/bmp" || i.mimetype == "image/png" || i.mimetype == "image/gif")
                        {
                            ici = i;
                        }
                    }
                    encoderparameters ep = new encoderparameters(1);
                    ep.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, (long)quality);

//保存缩略图
                    templateimage.save(filesaveurl, ici, ep);
                    //templateimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);

//释放资源
                    templateg.dispose();
                    templateimage.dispose();

pickedg.dispose();
                    pickedimage.dispose();
                }
            }

//释放资源
            initimage.dispose();
        }
        #endregion

#region 等比缩放
        /// <summary>
        /// 图片等比缩放
        /// </summary>
        /// <param name="postedfile">原图httppostedfile对象</param>
        /// <param name="savepath">缩略图存放地址</param>
        /// <param name="targetwidth">指定的最大宽度</param>
        /// <param name="targetheight">指定的最大高度</param>
        /// <param name="watermarktext">水印文字(为""表示不使用水印)</param>
        /// <param name="watermarkimage">水印图片路径(为""表示不使用水印)</param>
        public static void zoomauto(system.web.httppostedfile postedfile, string savepath, system.double targetwidth, system.double targetheight, string watermarktext, string watermarkimage)
        {
            //创建目录
            string dir = path.getdirectoryname(savepath);
            if (!directory.exists(dir))
                directory.createdirectory(dir);

//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
            system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);

//原图宽高均小于模版,不作处理,直接保存
            if (initimage.width <= targetwidth && initimage.height <= targetheight)
            {
                //文字水印
                if (watermarktext != "")
                {
                    using (system.drawing.graphics gwater = system.drawing.graphics.fromimage(initimage))
                    {
                        system.drawing.font fontwater = new font("黑体", 10);
                        system.drawing.brush brushwater = new solidbrush(color.white);
                        gwater.drawstring(watermarktext, fontwater, brushwater, 10, 10);
                        gwater.dispose();
                    }
                }

//透明图片水印
                if (watermarkimage != "")
                {
                    if (file.exists(watermarkimage))
                    {
                        //获取水印图片
                        using (system.drawing.image wrimage = system.drawing.image.fromfile(watermarkimage))
                        {
                            //水印绘制条件:原始图片宽高均大于或等于水印图片
                            if (initimage.width >= wrimage.width && initimage.height >= wrimage.height)
                            {
                                graphics gwater = graphics.fromimage(initimage);

//透明属性
                                imageattributes imgattributes = new imageattributes();
                                colormap colormap = new colormap();
                                colormap.oldcolor = color.fromargb(255, 0, 255, 0);
                                colormap.newcolor = color.fromargb(0, 0, 0, 0);
                                colormap[] remaptable = { colormap };
                                imgattributes.setremaptable(remaptable, coloradjusttype.bitmap);

float[][] colormatrixelements = {
                                   new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  0.0f,  0.0f,  0.5f, 0.0f},//透明度:0.5
                                   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                };

colormatrix wmcolormatrix = new colormatrix(colormatrixelements);
                                imgattributes.setcolormatrix(wmcolormatrix, colormatrixflag.default, coloradjusttype.bitmap);
                                gwater.drawimage(wrimage, new rectangle(initimage.width - wrimage.width, initimage.height - wrimage.height, wrimage.width, wrimage.height), 0, 0, wrimage.width, wrimage.height, graphicsunit.pixel, imgattributes);

gwater.dispose();
                            }
                            wrimage.dispose();
                        }
                    }
                }

//保存
                initimage.save(savepath, system.drawing.imaging.imageformat.jpeg);
            }
            else
            {
                //缩略图宽、高计算
                double newwidth = initimage.width;
                double newheight = initimage.height;

//宽大于高或宽等于高(横图或正方)
                if (initimage.width > initimage.height || initimage.width == initimage.height)
                {
                    //如果宽大于模版
                    if (initimage.width > targetwidth)
                    {
                        //宽按模版,高按比例缩放
                        newwidth = targetwidth;
                        newheight = initimage.height * (targetwidth / initimage.width);
                    }
                }
                //高大于宽(竖图)
                else
                {
                    //如果高大于模版
                    if (initimage.height > targetheight)
                    {
                        //高按模版,宽按比例缩放
                        newheight = targetheight;
                        newwidth = initimage.width * (targetheight / initimage.height);
                    }
                }

//生成新图
                //新建一个bmp图片
                system.drawing.image newimage = new system.drawing.bitmap((int)newwidth, (int)newheight);
                //新建一个画板
                system.drawing.graphics newg = system.drawing.graphics.fromimage(newimage);

//设置质量
                newg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
                newg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;

//置背景色
                newg.clear(color.white);
                //画图
                newg.drawimage(initimage, new system.drawing.rectangle(0, 0, newimage.width, newimage.height), new system.drawing.rectangle(0, 0, initimage.width, initimage.height), system.drawing.graphicsunit.pixel);

//文字水印
                if (watermarktext != "")
                {
                    using (system.drawing.graphics gwater = system.drawing.graphics.fromimage(newimage))
                    {
                        system.drawing.font fontwater = new font("宋体", 10);
                        system.drawing.brush brushwater = new solidbrush(color.white);
                        gwater.drawstring(watermarktext, fontwater, brushwater, 10, 10);
                        gwater.dispose();
                    }
                }

//透明图片水印
                if (watermarkimage != "")
                {
                    if (file.exists(watermarkimage))
                    {
                        //获取水印图片
                        using (system.drawing.image wrimage = system.drawing.image.fromfile(watermarkimage))
                        {
                            //水印绘制条件:原始图片宽高均大于或等于水印图片
                            if (newimage.width >= wrimage.width && newimage.height >= wrimage.height)
                            {
                                graphics gwater = graphics.fromimage(newimage);

//透明属性
                                imageattributes imgattributes = new imageattributes();
                                colormap colormap = new colormap();
                                colormap.oldcolor = color.fromargb(255, 0, 255, 0);
                                colormap.newcolor = color.fromargb(0, 0, 0, 0);
                                colormap[] remaptable = { colormap };
                                imgattributes.setremaptable(remaptable, coloradjusttype.bitmap);

float[][] colormatrixelements = {
                                   new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                   new float[] {0.0f,  0.0f,  0.0f,  0.5f, 0.0f},//透明度:0.5
                                   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                };

colormatrix wmcolormatrix = new colormatrix(colormatrixelements);
                                imgattributes.setcolormatrix(wmcolormatrix, colormatrixflag.default, coloradjusttype.bitmap);
                                gwater.drawimage(wrimage, new rectangle(newimage.width - wrimage.width, newimage.height - wrimage.height, wrimage.width, wrimage.height), 0, 0, wrimage.width, wrimage.height, graphicsunit.pixel, imgattributes);
                                gwater.dispose();
                            }
                            wrimage.dispose();
                        }
                    }
                }

//保存缩略图
                newimage.save(savepath, system.drawing.imaging.imageformat.jpeg);

//释放资源
                newg.dispose();
                newimage.dispose();
                initimage.dispose();
            }
        }

#endregion

#region 其它
        /// <summary>
        /// 判断文件类型是否为web格式图片
        /// (注:jpg,gif,bmp,png)
        /// </summary>
        /// <param name="contenttype">httppostedfile.contenttype</param>
        /// <returns></returns>
        public static bool iswebimage(string contenttype)
        {
            if (contenttype == "image/pjpeg" || contenttype == "image/jpeg" || contenttype == "image/gif" || contenttype == "image/bmp" || contenttype == "image/png")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

#endregion
    }//end class
}

(0)

相关推荐

  • C#图片按比例缩放的实现代码

    复制代码 代码如下: using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging; namespace Publics{    public class ImgHelper    {        public static void AdjustPhoto(int toWidth, int toHeight, string filePath, string fromFileName, stri

  • asp.net(c#)判断远程图片是否存在

    复制代码 代码如下: private int GetUrlError(string curl) { int num = 200; if(this.method==1) { HttpWebRequest request=(HttpWebRequest) WebRequest.Create(new Uri(curl)); ServicePointManager.Expect100Continue=false; try { ((HttpWebResponse)request.GetResponse()

  • C# 将字节流转换为图片的实例方法

    复制代码 代码如下: usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Drawing; usingSystem.IO; namespaceMicrosoft.Form.Base {     classImageToByte     {         /// <summary>         /// 图片转换成字节流         /// </s

  • ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)

    在做asp.net的Web开发的时候,我们经常会遇到一次性上传多个文件的需求.通常我们的解决方法是固定放多个上传文件框,这样的解决办法显然是不合理的,因为一次上传多个,就意味着数量不确定.因此我们就要让这些文件上传框动态添加,下面我以我做的一个图库管理中的上传图片的功能为例 先看效果: 打开的初始界面: 默认是上传一个图片,但当我们点"增加图片"按钮时可以实现选择多个图片及其描述同时上传,本功能限制一次最多只能上传8张,且每张图片大小不超过1M,这个大家可根据实际情况更改! 如图: 下

  • asp.net(C#)压缩图片,可以指定图片模板高宽

    复制代码 代码如下: //生成缩略图函数 //顺序参数:源图文件流.缩略图存放地址.模版宽.模版高 //注:缩略图大小控制在模版区域内 public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight) { //从文件取得图片对象,并使用流中嵌入的颜色管理信息 System.Dr

  • asp.net(c#)获取内容第一张图片地址的函数

    首先找到内容里面第一个<img标签的位置,然后找到从这个起的第一个>的位置,得到第一张图片的完整标签. 然后通过分隔空格得到图片的各个属性和属性值,提取src的值就是图片的地址 代码如下: 复制代码 代码如下: /// <summary> /// 获取文中图片地址 /// </summary> /// <param name="content">内容</param> /// <returns>地址字符串</r

  • asp.net(c#)实现从sqlserver存取二进制图片的代码

    下面说说主要实现思路: 1.存取图片 (1).将图片文件转换为二进制并直接存进sql server 复制代码 代码如下: //UploadHelper.cs /// <summary> /// 将图片转化为长二进制 /// </summary> /// <param name="photopath"></param> /// <returns></returns> public static Byte[] SetI

  • Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)

    本文实例讲述了Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果的方法.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.IO; public partial

  • C#识别出图片里的数字和字母

    一个图片识别小工具,原先主要是识别以前公司的软件注册码截图里的数字和字母(每次要一个一个框复制出来粘贴到注册器里,很麻烦!),因为注册码出现的字母和数字基本就那几个,所以识别库的范围设定的比较少. 原理和算法在代码中做了详细说明,功能存在很大的局限性,但我的想法是把这个思路和实现的办法共享出来. 源码下载地址: http://git.oschina.net/bobo2cj/iamge2text /* * 开发思路:图片灰度处理,二进制,然后和图片中的字二进制库精确对比 * * 获取字库:通过下面

  • asp.net(c#)编程实现将彩色图片变灰阶图片的方法示例

    本文实例讲述了asp.net(c#)编程实现将彩色图片变灰阶图片的方法.分享给大家供大家参考,具体如下: 代码如下: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI

  • c#实现图片二值化例子(黑白效果)

    C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: using System; using System.Drawing; namespace BMP2Grey { class Program { static void ToGrey(Bitmap img1) { for (int i = 0; i < img1.Width; i++) { for (int j = 0; j < img1.Height; j++) { Color pixelColor

  • asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例

    本文实例讲述了asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码.分享给大家供大家参考,具体如下: <%@ WebHandler Language="C#" Class="GetQRCode" %> using System; using System.Web; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtW

随机推荐