资源简介
C#使用GDI+处理图片,包括
1、按比例缩放图片
2、缩放到指定大小
3、裁剪缩放
4、四面裁剪图片
5、忽略比例直接缩放
代码片段和文件信息
enum AnchorPosition
{
Top
Center
Bottom
Left
Right
}
//1、按比例缩放图片
static Image ScaleByPercent(Image imgPhoto int Percent)
{
float nPercent = ((float)Percent/100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth destHeight
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto
new Rectangle(destXdestYdestWidthdestHeight)
new Rectangle(sourceXsourceYsourceWidthsourceHeight)
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
//2、缩放到指定大小
static Image FixedSize(Image imgPhoto int Width int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width Height
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto
new Rectangle(destXdestYdestWidthdestHeight)
new Rectangle(sourceXsourceYsourceWidthsourceHeight)
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
//3、裁剪缩放
static Image Crop(Image imgPhoto int Width
int Height AnchorPosition Anchor)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(
相关资源
- wpf仿WIN10画图程序
- C#仿QQ表情Demo
- C#简单计算器实现加减乘除
- DataTableHelper 用c# 对DataTable进行操作的
- C#中读取sql server的数据,并在datagri
- 各种基于Arcengine的利用c#实现的功能:
- c# 浮动工具栏实现
- c# arcengine 汽车在地图上移动的源代码
- C# HL7信息提取
- c#上位机接收STM32的数据实时显示存表
- c# chart 画波形
- floyd算法 C#实现
- 哲学家进餐问题C# 解决程序
- C#&SQLite 学生信息管理系统
- c# 反射获取传入对象的属性拼接sql语
- C#写的串口调试和校验和软件
- WPF动画界面以及WCF通讯框架实现即时
- CSharp波形显示控件源码.rar
- C#Socket 封装了Tcp/Udp传输字串、文件、
- C#随机生成发货地址,生成详细地址,
- 用c#编写的万年历 用c#编写的万年历
- 构建Delaunay三角网的c#源程序
- C#创建windows服务+Form+Web调用服务
- P2P之UDP穿透NAT的原理与C#实现
- 国家商用密码算法开放动态库及演示
- C# WinForm TreeListView控件用法
- c# picture下图片移动
- C# 简单的tcp服务器demo
- C#.net发送邮件完整源代码DEMO完整版
- c#的P2P聊天程序完整源码
评论
共有 条评论