資源簡介
C#實現將某張照片進行旋轉,目前我添加了可以瀏覽圖片,旋轉18度和26度,若想旋轉其他度數,直接復制button中函數體,修改度數即可。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?WindowsFormsApp1
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?const?string?IMAGEFILE?=?“G:\\RFID\\1.JPG“;
????????private?static?int?MyAngle?=?0;?//旋轉角度[-360360]??
????????private?void?Page_Load(object?sender?EventArgs?e)
????????{
????????????Image?img;
????????????FileStream?fs;
????????????fs?=?new?FileStream(IMAGEFILE?FileMode.Open?FileAccess.Read);
????????????img?=?Bitmap.FromStream(fs);
????????????fs.Close();
????????????pictureBox1.Image?=?img;
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
??????????????
????????????
????????????MyAngle?=?0;
????????????//MyAngle?+=?90;??
????????????//if?(MyAngle?>?360)?MyAngle?=?90;??
????????????pictureBox1.Image?=?RotateImg(GetSourceImg(IMAGEFILE)?MyAngle);
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????MyAngle?=?90;
????????????//MyAngle?+=?-90;??
????????????//if?(MyAngle?-360)?MyAngle?=?-90;??
????????????//pictureBox1.Image?=?RotateImg(Bitmap.FromFile(IMAGEFILE)?MyAngle);??
????????????pictureBox1.Image?=?RotateImg(IMAGEFILE?MyAngle);
????????}
????????
???????
????????????public?Image?RotateImg(Image?b?int?angle)
????????{
????????????angle?=?angle?%?360;
????????????//弧度轉換??
????????????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);
????????????//構造圖像顯示區域:讓圖像的中心與窗口的中心點一致??
????????????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(360?-?angle);
????????????//恢復圖像在水平和垂直方向的平移??
????????????g.TranslateTransform(-center.X?-center.Y);
????????????g.DrawImage(b?rect);
????????????//重至繪圖的所有變換?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????22??2019-01-08?00:19??WindowsFormsApp1\.git\COMMIT_EDITMSG
?????文件????????652??2019-01-08?00:19??WindowsFormsApp1\.git\config
?????文件?????????73??2019-01-08?00:19??WindowsFormsApp1\.git\desc
?????文件?????????23??2019-01-08?00:19??WindowsFormsApp1\.git\HEAD
?????文件????????478??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\applypatch-msg.sample
?????文件????????896??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\commit-msg.sample
?????文件????????189??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\post-update.sample
?????文件????????424??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\pre-applypatch.sample
?????文件???????1642??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\pre-commit.sample
?????文件???????1348??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\pre-push.sample
?????文件???????4898??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\pre-reba
?????文件????????544??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\pre-receive.sample
?????文件???????1239??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\prepare-commit-msg.sample
?????文件???????3610??2019-01-08?00:19??WindowsFormsApp1\.git\hooks\update.sample
?????文件???????1342??2019-01-08?00:19??WindowsFormsApp1\.git\index
?????文件????????240??2019-01-08?00:19??WindowsFormsApp1\.git\info\exclude
?????文件????????342??2019-01-08?00:19??WindowsFormsApp1\.git\logs\HEAD
?????文件????????342??2019-01-08?00:19??WindowsFormsApp1\.git\logs\refs\heads\master
?????文件????????150??2019-01-08?00:19??WindowsFormsApp1\.git\ms-persist.xm
?????文件????????309??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????751??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????186??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????186??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件???????2112??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????491??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????718??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????201??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????163??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件????????460??2019-01-08?00:19??WindowsFormsApp1\.git\ob
?????文件?????????89??2019-01-08?00:19??WindowsFormsApp1\.git\ob
............此處省略85個文件信息
- 上一篇:C#語言的網絡爬蟲
- 下一篇:C#6.0學習筆記隨書代碼
評論
共有 條評論