代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
图片缩放
作者:
/ 发布于
2011/1/12
/
588
<div>************************************************************// //下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着 //************************************************************// /// <summary> /// 缩小图片 /// </summary> /// <param name="strOldPic">源图文件名(包括路径)</param> /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param> /// <param name="intWidth">缩小至宽度</param> /// <param name="intHeight">缩小至高度</param> public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight) { System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic); } catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } } /// <summary> /// 按比例缩小图片,自动计算高度 /// </summary> /// <param name="strOldPic">源图文件名(包括路径)</param> /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param> /// <param name="intWidth">缩小至宽度</param> public void SmallPic(string strOldPic, string strNewPic, int intWidth) { System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intHeight=(intWidth / objPic.Width) * objPic.Height; objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic); } catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } } /// <summary> /// 按比例缩小图片,自动计算宽度 /// </summary> /// <param name="strOldPic">源图文件名(包括路径)</param> /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param> /// <param name="intHeight">缩小至高度</param> public void SmallPic(string strOldPic, string strNewPic, int intHeight) { System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intWidth=(intHeight / objPic.Height) * objPic.Width; objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic); } catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } } //************************************************************//</div> <div> 图片缩放(2)效果最好的。</div> <div> 图片无损缩放</div> <div>* 一个图片缩放的类 * ********************************************************************/ using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D;</div> <div>namespace ZHT.Utility.Share { public class Thumbnail { public Thumbnail() { } <div> /// <SUMMARY> /// 图片缩放 /// </SUMMARY> /// <PARAM name="sourceFile">图片源路径</PARAM> /// <PARAM name="destFile">缩放后图片输出路径</PARAM> /// <PARAM name="destHeight">缩放后图片高度</PARAM> /// <PARAM name="destWidth">缩放后图片宽度</PARAM> /// <RETURNS></RETURNS> public static bool GetThumbnail(string sourceFile, string destFile, int destHeight, int destWidth) { System.Drawing.Image imgSource = System.Drawing.Image.FromFile(sourceFile); System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat; int sW = 0, sH = 0; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height;</div> <div> if(sHeight>destHeight || sWidth>destWidth) { if((sWidth*destHeight)>(sHeight*destWidth)) { sW = destWidth; sH = (destWidth*sHeight)/sWidth; } else { sH = destHeight; sW = (sWidth*destHeight)/sHeight; } } else { sW = sWidth; sH = sHeight; } <div> Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.WhiteSmoke);</div> <div> // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic;</div> <div> g.DrawImage(imgSource, new Rectangle((destWidth-sW)/2,(destHeight-sH)/2, sW, sH),0,0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose();</div> <div> // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100;</div> <div> EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam;</div> <div> try { //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[x];//设置JPEG编码 break; } } <div> if (jpegICI != null) { outBmp.Save(destFile, jpegICI, encoderParams); } else { outBmp.Save(destFile, thisFormat); } <div> return true; } catch { return false; } finally { imgSource.Dispose(); outBmp.Dispose(); } } } } <div> 3.图片的裁剪</div> <div> C#图片处理之:图片缩放和剪裁 作者:ki1381</div> <div> 其实在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。</div> <div> /// <summary> /// Resize图片 /// </summary> /// <param name="bmp">原始Bitmap</param> /// <param name="newW">新的宽度</param> /// <param name="newH">新的高度</param> /// <param name="Mode">保留着,暂时未用</param> /// <returns>处理以后的图片</returns> public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode) { try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b);</div> <div> // 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic;</div> <div> g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose();</div> <div> return b; } catch { return null; } } <div> // ===============================</div> <div> /// <summary> /// 剪裁 -- 用GDI+ /// </summary> /// <param name="b">原始Bitmap</param> /// <param name="StartX">开始坐标X</param> /// <param name="StartY">开始坐标Y</param> /// <param name="iWidth">宽度</param> /// <param name="iHeight">高度</param> /// <returns>剪裁后的Bitmap</returns> public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight) { if (b == null) { return null; } <div> int w = b.Width; int h = b.Height;</div> <div> if (StartX >= w || StartY >= h) { return null; } <div> if (StartX + iWidth > w) { iWidth = w - StartX; } <div> if (StartY + iHeight > h) { iHeight = h - StartY; } <div> try { Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);</div> <div> Graphics g = Graphics.FromImage(bmpOut); g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel); g.Dispose();</div> <div> return bmpOut; } catch { return null; } } <div>注意到区别了吗?提示,g.DrawImage中第二个new Rectangle。</div> <div>目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。很容易吧。</div> <div> 4.浮雕图片</div> <div> private void button1_Click(object sender, EventArgs e) { //以浮雕效果显示图像 try { int Height = this.pictureBox1.Image.Height; int Width = this.pictureBox1.Image.Width; Bitmap newBitmap = new Bitmap(Width, Height); Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image; Color pixel1, pixel2; for (int x = 0; x < Width - 1; x++) { for (int y = 0; y < Height - 1; y++) { int r = 0, g = 0, b = 0; pixel1 = oldBitmap.GetPixel(x, y); pixel2 = oldBitmap.GetPixel(x + 1, y + 1); r = Math.Abs(pixel1.R - pixel2.R + 128); g = Math.Abs(pixel1.G - pixel2.G + 128); b = Math.Abs(pixel1.B - pixel2.B + 128); if (r > 255) r = 255; if (r < 0) r = 0; if (g > 255) g = 255; if (g < 0) g = 0; if (b > 255) b = 255; if (b < 0) b = 0; newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b)); } } this.pictureBox1.Image = newBitmap; } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } <div></div>
试试其它关键字
图片缩放
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3