雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

利用C#代碼實現(xiàn)圖片旋轉(zhuǎn)360度

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

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像進(jìn)行旋轉(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)));
   //目標(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;
   //計算偏移量 
   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)系,謝謝對我們的支持!

上一篇:C#編程總結(jié)(六)詳解異步編程

欄    目:C#教程

下一篇:淺談C#手機號換成111XXXX1111 這種顯示的解決思路

本文標(biāo)題:利用C#代碼實現(xiàn)圖片旋轉(zhuǎn)360度

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6155.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有