首页 技术 正文
技术 2022年11月7日
0 收藏 834 点赞 295 浏览 2238 个字

这几天开发一个程序,需要将一个图片旋转360度然后每一个角度保存下来。刚开始本来想着是让美工弄的,但是让一个美工手动转360度,她会喷你一脸。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
namespace 图片旋转程序
{
public class ImageHelper
{
/// <summary>
/// 以逆时针为方向对图像进行旋转
/// </summary>
/// <param name="b">位图流</param>
/// <param name="angle">旋转角度[0,360](前台给的)</param>
/// <returns></returns>
public Image RotateImg(Image b, int angle, string file)
{
angle = angle % ;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = b.Width;
int h = b.Height;
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
//目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
Point Offset = new Point((W - w) / , (H - h) / );
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / , rect.Y + rect.Height / );
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//保存旋转后的图片
dsImage.Save(@"D:\img\" + Path.GetFileNameWithoutExtension(file) + "\\" + angle + ".png", System.Drawing.Imaging.ImageFormat.Png);
return dsImage;
}
public Image RotateImg(string filename, int angle, string file)
{
return RotateImg(GetSourceImg(filename), angle, file);
}
public Image GetSourceImg(string filename)
{
Image img;
img = Bitmap.FromFile(filename);
return img;
}
}
class Program
{
static void Main(string[] args)
{
string[] strArr = Directory.GetFiles(@"D:\img");
foreach (string file in strArr)
{
Console.WriteLine(file); //输出E:\123\新建文本文件.txt
Console.WriteLine(Path.GetFileNameWithoutExtension(file));
//如果要保存的目录不存在,则先创建
if (!Directory.Exists(@"D:\img\" + Path.GetFileNameWithoutExtension(file)))
{
Directory.CreateDirectory(@"D:\img\" + Path.GetFileNameWithoutExtension(file));
}
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
Image img = Bitmap.FromStream(fs);
ImageHelper IH = new ImageHelper();
for (int i = ; i <= ; i++)
{
IH.RotateImg(img, i, file);
}
fs.Close();
}
Console.ReadKey();
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289