利用C#代碼實(shí)現(xiàn)圖片旋轉(zhuǎn)360度
更新時(shí)間:2016年11月21日 10:39:20 作者:逆心
本文介紹利用C#代碼實(shí)現(xiàn)圖片旋轉(zhuǎn)360度,具體實(shí)例代碼已附上,僅供大家參考,希望對(duì)大家有所幫助
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> /// 以逆時(shí)針為方向?qū)D像進(jìn)行旋轉(zhuǎn) /// </summary> /// <param name="b">位圖流</param> /// <param name="angle">旋轉(zhuǎn)角度[0,360](前臺(tái)給的)</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))); //目標(biāo)位圖 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; //計(jì)算偏移量 Point Offset = new Point((W - w) / 2, (H - h) / 2); //構(gòu)造圖像顯示區(qū)域:讓圖像的中心與窗口的中心點(diǎn)一致 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(); } } }
以上所述是本文的全部?jī)?nèi)容,有問題的可以和小編聯(lián)系,謝謝對(duì)腳本之家的支持!
您可能感興趣的文章:
- c# WinForm制作圖片編輯工具(圖像拖動(dòng)、縮放、旋轉(zhuǎn)、摳圖)
- C#使用opencv截取旋轉(zhuǎn)矩形區(qū)域圖像的實(shí)現(xiàn)示例
- C# 使用 GDI+ 實(shí)現(xiàn)添加中心旋轉(zhuǎn)(任意角度)的文字
- C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法
- C#中圖片旋轉(zhuǎn)和翻轉(zhuǎn)(RotateFlipType)用法分析
- C#控制圖像旋轉(zhuǎn)和翻轉(zhuǎn)的方法
- C# VTK 移動(dòng)旋轉(zhuǎn)交互功能實(shí)現(xiàn)
相關(guān)文章
c#實(shí)現(xiàn)最簡(jiǎn)潔的快速排序(你絕對(duì)可以看懂)
這篇文章主要給大家介紹了關(guān)于利用c#實(shí)現(xiàn)如何最簡(jiǎn)潔的快速排序,實(shí)現(xiàn)的方法你絕對(duì)可以看懂,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05C# 封裝HtmlHelper組件:BootstrapHelper
這篇文章主要介紹了C# 封裝HtmlHelper組件之BootstrapHelper 的相關(guān)資料,需要的朋友可以參考下2016-08-08C#中的SQLCommand命令與DbTransaction事務(wù)處理
這篇文章介紹了C#中的SQLCommand命令與DbTransaction事務(wù)處理,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#并發(fā)編程之a(chǎn)sync和await關(guān)鍵字詳解
對(duì)于?async?和?await?兩個(gè)關(guān)鍵字,對(duì)于一線開發(fā)人員再熟悉不過了,到處都是它們的身影,下面小編就來和大家記錄匯總下它們的使用吧2023-07-07Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04