对图片处理C#相关代码 压缩指定图片

来源:互联网 发布:linux vim中文手册 编辑:程序博客网 时间:2024/04/30 13:51

1.string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();//图片地址后缀

2   if (fileUrl.LastIndexOf('\\') != fileUrl.Length - 1)//配置路径没有以“\”结尾
                        fileUrl += "\\" + fileUrl0;
                    else
                        fileUrl += fileUrl0;


                    if (saveFileUrl.LastIndexOf('/') != saveFileUrl.Length - 1)//配置路径没有以“/”结尾
                        saveFileUrl += "/" + saveFileUrl0;
                    else
                        saveFileUrl += saveFileUrl0;

3.FileOperating.fileUpLoad(fileUrl, FileUpload1);上传图片

FileOperating.fileDelete(cpath, false);//删除图片

4.  /// <summary>
    /// 把指定的图片按指定的大小保存到指定的文件中
    /// </summary>
    /// <param name="imagepath">源图地址</param>
    /// <param name="compresspath">压缩图保存地址</param>
    /// <param name="width">压缩后图片的宽度</param>
    /// <param name="height">压缩后图片的高度</param>
    /// <returns>保存是否成功</returns>
    /// 一般错误:到保存图片时出现GDI错误——一般为保存图片的地址不正确
    protected bool SaveCompress(string imagepath, string compresspath, int width, int height)
    {
        bool issuccess = false;


        //如果文件已存在则删除该文件
        if (File.Exists(compresspath))
        {
            FileOperating.fileDelete(compresspath, false);
        }
        //判断文件是否存在
        if (File.Exists(imagepath))
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(imagepath);//获取源图


            //判断保存图片的文件夹是否存在,若不存在则创建
            string filepath = compresspath.Substring(0, compresspath.LastIndexOf("\\"));
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }


            System.Drawing.Image simage = new System.Drawing.Bitmap(width, height);//新建一张图片
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(simage);//新建一张画布
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;


            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.Drawing.Color.Transparent);//清空画布以透明填充
            g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);




            try
            {
                simage.Save(compresspath, image.RawFormat);//保存压缩图


                issuccess = true;
                log.Warn("保存简图成功!");
            }
            catch (Exception e)
            {
                log.Error("保存简图错误:" + e.Message);
            }
            finally
            {
                //释放资源
                image.Dispose();
                simage.Dispose();
                g.Dispose();
                log.Warn("保存简图:源图地址:" + imagepath + "简图地址:" + compresspath);
            }
        }


        return issuccess;
    }

5.   DirectoryInfo dirInfo3 = new DirectoryInfo(fileDesnsuolue);


                    if (!dirInfo3.Exists)           //文件目录不存在,创建一个目录
                    {
                        dirInfo2.Create();
                    }



原创粉丝点击