/// <summary>
/// 图片转成圆角方法二
/// </summary>
private Bitmap WayTwo(Bitmap bitmap)
{
//using (Image i = new Bitmap(file))
using (Image i = bitmap)
{
Bitmap b = new Bitmap(i.Width, i.Height);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage(i, 0, 0, b.Width, b.Height);
int r = Math.Min(b.Width, b.Height) / 2;
PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F);
for (int h = 0; h < b.Height; h++)
for (int w = 0; w < b.Width; w++)
if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2)))
{
b.SetPixel(w, h, Color.Transparent);
}
}
return b;
}
} /// <summary>
/// 合并图片
/// </summary>
/// <param name="imgBack"></param>
/// <param name="img"></param>
/// <returns></returns>
public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
{
Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height);
Graphics g = Graphics.FromImage(bmp); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
//白色边框
// g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 + xDeviation - 1, imgBack.Height / 2 - img.Height / 2 + yDeviation - 1, img.Width + 2, img.Height + 2);
//填充图片 //设置高质量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality; g.Clear(Color.Transparent); //防止出现渐变
var imgAtt = new ImageAttributes();
imgAtt.SetWrapMode(WrapMode.TileFlipXY); var x = imgBack.Width / 2 - img.Width / 2 + xDeviation;
var y = imgBack.Height / 2 - img.Height / 2 + yDeviation;
var width = img.Width;
var height = img.Height;
// g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height / 2 - img.Height / 2 + yDeviation,img.Width , img.Height);
g.DrawImage(img, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel, imgAtt); GC.Collect();
return bmp;
} /// <summary>
/// 从大图中截取一部分图片
/// </summary>
/// <param name="fromImage">来源图片</param>
/// <param name="offsetX">从偏移X坐标位置开始截取</param>
/// <param name="offsetY">从偏移Y坐标位置开始截取</param>
/// <param name="width">保存图片的宽度</param>
/// <param name="height">保存图片的高度</param>
/// <returns></returns>
public Image ResizeImage(Image fromImage, int offsetX, int offsetY, int width, int height)
{
//创建新图位图
Bitmap bitmap = new Bitmap(width, height);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(offsetX, offsetY, width, height), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
////保存图片
//saveImage.Save(toImagePath, ImageFormat.Png);
////释放资源
//saveImage.Dispose();
//graphic.Dispose();
//bitmap.Dispose();
return saveImage;
} /// <summary>
/// 按照指定大小缩放图片,但是为了保证图片宽高比自动截取
/// </summary>
/// <param name="srcPath">原图片路径</param>
/// <param name="destWidth">目标图片宽</param>
/// <param name="destHeight">目标图片高</param>
/// <returns></returns>
public static Bitmap SizeImageWithOldPercent(Bitmap srcPath, int destWidth, int destHeight)
{
Image srcImage = 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.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.Clear(Color.Transparent); //防止出现渐变
var imgAtt = new ImageAttributes();
imgAtt.SetWrapMode(WrapMode.TileFlipXY); // g.DrawImage(cutedImage, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel);
// 在画板的指定位置画图
g.DrawImage(cutedImage, new Rectangle(0, 0, destWidth, destHeight), 0, 0, cutedImage.Width, cutedImage.Height, GraphicsUnit.Pixel, imgAtt);
// 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">目标图片高度高度</param>
/// <returns>剪裁后的Bitmap</returns>
public 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.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.Clear(Color.Transparent); // 将原始图片画到目标画布上
// g.DrawImage(image,new Rectangle(0, 0, destWidth, destHeight), new Rectangle(StartX, StartY, destWidth, destHeight), GraphicsUnit.Pixel); //防止出现渐变(生成高质量不模糊的图片)
var imgAtt = new ImageAttributes();
imgAtt.SetWrapMode(WrapMode.TileFlipXY);
// 在画板的指定位置画图
g.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), StartX, StartY, destWidth, destHeight, GraphicsUnit.Pixel, imgAtt); g.Dispose(); return bmpOut;
}
catch
{
return null;
} }
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
{
System.Drawing.Image imgSource = b;
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;
if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
return b;
//sW = sWidth;
//sH = sHeight;
} Bitmap outBmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality; g.Clear(Color.Transparent); //防止出现渐变
var imgAtt = new ImageAttributes();
imgAtt.SetWrapMode(WrapMode.TileFlipXY);
//在画板的指定位置画图
g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0,
imgSource.Width, imgSource.Height, GraphicsUnit.Pixel, imgAtt); g.Dispose();
// 以下代码为保存图片时,设置压缩质量
//EncoderParameters encoderParams = new EncoderParameters();
//long[] quality = new long[1];
//quality[0] = 100;
//EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
//encoderParams.Param[0] = encoderParam; imgSource.Dispose();
return outBmp;
}

  

以下是图片处理常用的一些方法;

C#常用的图片处理方法-图片剪切、图片压缩、多图合并代码的更多相关文章

  1. CSS——图片替换方法:Fahrner图片替换法(FIR)

    Html: <h1 id="fir"><span>Fahrner Image Replacement</span></h1> CSS ...

  2. MATLAB(4)——图片保存方法汇总及常用指令

    作者:桂. 时间:2017-03-03  19:30:03 链接:http://www.cnblogs.com/xingshansi/p/6498318.html 前言 本文为MATLAB系列第四篇. ...

  3. [转]常用iOS图片处理方法

    自:http://blog.sina.com.cn/s/blog_8988732e0100xcx1.html  ========== (one) UIImage 图像 等比例缩放=========== ...

  4. android图片处理方法(不断收集中)

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  5. Android 图片处理方法

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  6. android图片处理方法(转)

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  7. php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明

    php有一款插件叫做imagemagick,功能很强大,提供了图片的很多操作,图片剪切.压缩.合并.插入文本.背景色透明等.并且有api方法调用和命令行操作两种方式,如果只是简单处理的话建议api方法 ...

  8. JAVA中pdf转图片的方法

    JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...

  9. Hexo博客插入图片的方法

    Hexo博客插入图片的方法 hexo图片blog hexo blog 插入图片的方法总结 hexo 的blog 内容是根据 markdown 文件的内容生成的html文件, 生成的文件全部在 /pub ...

随机推荐

  1. flume参数解析+启动参数解析

    flume参数: #example.conf:单节点Flume配置 #命名此代理上的组件 a1.sources = r1 a1.sinks = k1 a1.channels = c1 #描述/配置源 ...

  2. ES6高级技巧(二)

    Array.from const cities = [ { name: 'Milan', visited: 'no' }, { name: 'Palermo', visited: 'yes' }, { ...

  3. Excel 简单使用

    1.Excel复制上一行 注意鼠标的样子 2.删除多行 删除之后如图所示: 删除多列也是同样的操作 3.日期格式不能按照数据库的形式进行输入 数字的位数太多输入之后改变了数字,可以设置为文本格式,进行 ...

  4. my first blog by cnblogs

    #include <stdio.h> int main() { printf("hello everyone."); ; } 上面为我的第一个C语言测试代码,仅供初学者 ...

  5. ubuntu docker 搭建 mongodb,开启授权访问 redis,mysql mssql 备份还原

    命令安装docker 如果您想从Ubuntu存储库安装docker版本,则可以运行下面的apt命令. sudo apt install docker.io等到安装完成后,您可以启动Docker并使用s ...

  6. kubectl 创建 Pod 背后到底发生了什么?

    原文链接:kubectl 创建 Pod 背后到底发生了什么? 想象一下,如果我想将 nginx 部署到 Kubernetes 集群,我可能会在终端中输入类似这样的命令: $ kubectl run - ...

  7. Go语言入门——hello world

    Go 语言源代码文件扩展名是.go. 知识点:1. go语言代码的第1行必须声明包2. 入口的go语言代码(包含main函数的代码文件)的包必须是main,否则运行go程序会显示go run: can ...

  8. 使用el-dialog时,报错“Unknown custom element:<el-dialog> did you register the component correctly?...make sure to provide the 'name' option”

    初学vue时,曾遇到一个无语的问题,在用el-dialog时一直显示没有导入,结果发现是因为没有把element ui引入到项目里. 需进行以下步骤: 1.执行: npm install elemen ...

  9. .net core使用ocelot---第二篇 身份验证

    简介原文链接      .net core使用ocelot---第一篇 简单使用 接上文,我将继续介绍使用asp.net core 创建API网关,主要介绍身份验证(authentication )相 ...

  10. C:\Program不是内部或外部命令,也不是可运行的程序或批处理文件。

    问题描述:C:\Program不是内部或外部命令,也不是可运行的程序或批处理文件. 解决办法:C:\"Program Files"\具体文件目录. 具体场景:在cmd或者批处理文件 ...