控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

用于图片切割,图片压缩,缩略图的生成

作者:佚名 出处:互联网 2011年08月11日 阅读:

用于图片切割,图片压缩,缩略图的生成
 
 /**//// <summary>
    /// 图片切割函数
    /// </summary>
    /// <param name="sourceFile">原始图片文件</param>
    /// <param name="xNum">在X轴上的切割数量</param>
    /// <param name="yNum">在Y轴上的切割数量</param>
    /// <param name="quality">质量压缩比</param>
    /// <param name="outputFile">输出文件名,不带后缀</param>
    /// <returns>成功返回true,失败则返回false</returns>
    public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile)
    {
        try
        {
            long imageQuality                        =    quality;
            Bitmap sourceImage                       =    new Bitmap(sourceFile);
            ImageCodecInfo myImageCodecInfo          =    GetEncoderInfo("image/jpeg");
            Encoder myEncoder                        =    Encoder.Quality;
            EncoderParameters myEncoderParameters    =    new EncoderParameters(1);
            EncoderParameter myEncoderParameter      =    new EncoderParameter(myEncoder, imageQuality);
            myEncoderParameters.Param[0]             =    myEncoderParameter;
            float xWidth                             =    sourceImage.Width / xNum;
            float yWidth                             =    sourceImage.Height / yNum;
            String outputImage                       =    "";
           
            for (int countY = 0; countY < yNum; countY++)
                for (int countX = 0; countX < xNum; countX++)
                {

                    RectangleF cloneRect             =    new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth);
                    Bitmap newImage                  =    sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb);
                    outputImage                      =    outputFile + countX + countY + ".jpg";
                    newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters);

                }
            return true;
        }
        catch
        {
            return false;
        }

    }
    #endregion imageCut

    imageCompress#region imageCompress
    /**//// <summary>
    /// 图片压缩函数
    /// </summary>
    /// <param name="sourceFile">原始图片文件</param>
    /// <param name="quality">质量压缩比</param>
    /// <param name="ouputFile">输出文件名,请用 .jpg 后缀 </param>
    /// <returns>成功返回true,失败则返回false</returns>
    public static bool imageCompress(String sourceFile,long quality,String outputFile)
    {
        try
        {
            long imageQuality                      =    quality;
            Bitmap sourceImage                     =    new Bitmap(sourceFile);
            ImageCodecInfo myImageCodecInfo        =    GetEncoderInfo("image/jpeg");
            Encoder myEncoder                      =    Encoder.Quality;
            EncoderParameters myEncoderParameters  =    new EncoderParameters(1);
            EncoderParameter myEncoderParameter    =    new EncoderParameter(myEncoder, imageQuality);
            myEncoderParameters.Param[0]           =    myEncoderParameter;
          
            sourceImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
            return true;

        }
        catch
        {
            return false;
        }

    }
    #endregion imageCompress

    getThumImage#region getThumImage
    /**//// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="sourceFile">原始图片文件</param>
    /// <param name="quality">质量压缩比</param>
    /// <param name="multiple">收缩倍数</param>
    /// <param name="outputFile">输出文件名</param>
    /// <returns>成功返回true,失败则返回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");
            Encoder myEncoder                      =   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

    ImageCodecInfo#region ImageCodecInfo
    /**//// <summary>
    /// 获取图片编码信息
    /// </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;
    }


 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统