您的位置:首页 > 编程语言 > C#

C#学习笔记之以指定的大小缩放图片

2017-08-03 10:04 169 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace QingdaoZenius
{
class ScalPhoto
{
/// <summary>
/// 按照指定大小缩放图片,但是为了保证图片宽高比自动截取
/// </summary>
/// <param name="srcPath">原图片路径</param>
/// <param name="destWidth">目标图片宽</param>
/// <param name="destHeight">目标图片高</param>
/// <returns></returns>
public static Bitmap SizeImageWithOldPercent(string srcPath, int destWidth, int destHeight)
{
Image srcImage = Image.FromFile(srcPath);
try
{
// 要截取图片的宽度(原始宽度)
int srcWidth = srcImage.Width;
// 要截取图片的高度(原始高度)
int srcHeight = srcImage.Height;
// 截取开始横坐标
int newX = 0;
// 截取开始纵坐标
int newY = 0;

// 截取比例
double whPercent = ((double)destWidth / (double)destHeight) * ((double)srcImage.Height / (double)srcImage.Width);
if (whPercent > 1)
{
// 当前图片宽度对于要截取比例过大时
srcWidth = int.Parse(Math.Round(srcImage.Width / whPercent).ToString());
}
else if (whPercent < 1)
{
// 当前图片高度对于要截取比例过大时
srcHeight = int.Parse(Math.Round(srcImage.Height * whPercent).ToString());
}

if (srcWidth != srcImage.Width)
{
// 宽度有变化时,调整开始截取的横坐标
newX = Math.Abs(int.Parse(Math.Round(((double)srcImage.Width - srcWidth) / 2).ToString()));
}
else if (srcHeight == srcImage.Height)
{
// 高度有变化时,调整开始截取的纵坐标
newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)srcHeight) / 2).ToString()));
}

// 将原始图片画到目标画布上,返回目标图片
Bitmap cutedImage = CutImage(srcImage, newX, newY, srcWidth, srcHeight);

// 在创建一个新的画布
Bitmap bmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(bmp);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.Default;
g.DrawImage(cutedImage, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel);
g.Dispose();

return bmp;
}
catch (Exception)
{
return null;
}
}

/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="image">原始图片</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="destWidth">目标图片宽度</param>
/// <param name="destHeight">目标图片高度
4000
高度</param>
/// <returns>剪裁后的Bitmap</returns>
private static Bitmap CutImage(Image image, int StartX, int StartY, int destWidth, int destHeight)
{
int srcWidth = image.Width;
int srcHeight = image.Height;
if (StartX >= srcWidth || StartY >= srcHeight)
{
// 开始截取坐标过大时,结束处理
return null;
}

if (StartX + destWidth > srcWidth)
{
// 宽度过大时只截取到最大大小
destWidth = srcWidth - StartX;
}

if (StartY + destHeight > srcHeight)
{
// 高度过大时只截取到最大大小
destHeight = srcHeight - StartY;
}

try
{
// 根据目标图片的大小,实例化一个画布
Bitmap bmpOut = new Bitmap(destWidth, destHeight);
// 实例化一个画笔
Graphics g = Graphics.FromImage(bmpOut);
// 将原始图片画到目标画布上
g.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(StartX, StartY, destWidth, destHeight), GraphicsUnit.Pixel);
g.Dispose();

return bmpOut;
}
catch
{
return null;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: