C# 图片缩放放大剪切代码
asp.net C#圖片縮放放大剪切代碼
本人在網上找了很多資料,整合了下..分享上來 ~
net c#圖片縮放放大剪切代碼
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
}
比如相冊功能,通常用戶上傳相片后我們都會針對該相片再生成一張縮略圖,用于其它頁面上的列表顯示。隨便看一下,大部分網站基 本都是將原圖等比縮放來生成縮略圖。但完美主義者會發現一些問題,比如顯示排版時想讓相片縮略圖列表非常統一、整齊、和美觀,比如要求每張縮略圖大小固定 為120 x 90且不拉伸變形怎么辦?再比如用戶頭像如何讓縮略圖比原圖更清晰?或是如何在上傳的圖片下加一個半透明的logo水印?
ok,本文根據自己的項目代碼描述以上問題的解決方案,所謂c#圖片處理高級應用,感覺有點標題黨了,這些功能并無多大技術含量。全部基于.net framework類庫完成,代碼中包含了c#圖片處理的一些基礎知識,與大家分享,個人能力有限,不足之處還請及時指正
轉載于:https://www.cnblogs.com/xiong-QQ357253950/archive/2013/05/23/3094917.html
總結
以上是生活随笔為你收集整理的C# 图片缩放放大剪切代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我心目中理想的开源软件
- 下一篇: c# char unsigned_dll