利用C#代碼實現(xiàn)圖片旋轉(zhuǎn)360度
更新時間:2016年11月21日 10:39:20 作者:逆心
本文介紹利用C#代碼實現(xiàn)圖片旋轉(zhuǎn)360度,具體實例代碼已附上,僅供大家參考,希望對大家有所幫助
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
namespace 圖片旋轉(zhuǎn)程序
{
public class ImageHelper
{
/// <summary>
/// 以逆時針為方向?qū)D像進行旋轉(zhuǎn)
/// </summary>
/// <param name="b">位圖流</param>
/// <param name="angle">旋轉(zhuǎn)角度[0,360](前臺給的)</param>
/// <returns></returns>
public Image RotateImg(Image b, int angle, string file)
{
angle = angle % 360;
//弧度轉(zhuǎn)換
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) / 2, (H - h) / 2);
//構(gòu)造圖像顯示區(qū)域:讓圖像的中心與窗口的中心點一致
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(angle);
//恢復(fù)圖像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
//重至繪圖的所有變換
g.ResetTransform();
g.Save();
g.Dispose();
//保存旋轉(zhuǎn)后的圖片
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));
//如果要保存的目錄不存在,則先創(chuàng)建
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 = 1; i <= 360; i++)
{
IH.RotateImg(img, i, file);
}
fs.Close();
}
Console.ReadKey();
}
}
}
以上所述是本文的全部內(nèi)容,有問題的可以和小編聯(lián)系,謝謝對腳本之家的支持!
相關(guān)文章
C# 封裝HtmlHelper組件:BootstrapHelper
這篇文章主要介紹了C# 封裝HtmlHelper組件之BootstrapHelper 的相關(guān)資料,需要的朋友可以參考下2016-08-08
C#中的SQLCommand命令與DbTransaction事務(wù)處理
這篇文章介紹了C#中的SQLCommand命令與DbTransaction事務(wù)處理,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#并發(fā)編程之a(chǎn)sync和await關(guān)鍵字詳解
對于?async?和?await?兩個關(guān)鍵字,對于一線開發(fā)人員再熟悉不過了,到處都是它們的身影,下面小編就來和大家記錄匯總下它們的使用吧2023-07-07

