C#正方形圖片的繪制方法
更新時間:2018年10月16日 08:55:45 作者:薛定諤家的貓
這篇文章主要為大家詳細介紹了C#正方形圖片的繪制方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#繪制正方形圖片的的具體代碼,供大家參考,具體內(nèi)容如下
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace treads
{
/// <summary>
/// 制作小正方形
/// </summary>
class Class3
{
private string srcFileName = @"x";//獲取圖片的路徑
private string srcFileName1 = @"x";//要保持圖片的新路徑
/// <summary>
/// 保存圖片
/// </summary>
/// <param name="image">Image 對象</param>
/// <param name="savePath">保存路徑</param>
/// <param name="ici">指定格式的編解碼參數(shù)</param>
private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
{
//設置 原圖片 對象的 EncoderParameters 對象
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)100));
image.Save(savePath, ici, parameters);
parameters.Dispose();
}
/// <summary>
/// 獲取圖像編碼解碼器的所有相關信息
/// </summary>
/// <param name="mimeType">包含編碼解碼器的多用途網(wǎng)際郵件擴充協(xié)議 (MIME) 類型的字符串</param>
/// <returns>返回圖像編碼解碼器的所有相關信息</returns>
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType)
return ici;
}
return null;
}
/// <summary>
/// 計算新尺寸
/// </summary>
/// <param name="width">原始寬度</param>
/// <param name="height">原始高度</param>
/// <param name="maxWidth">最大新寬度</param>
/// <param name="maxHeight">最大新高度</param>
/// <returns></returns>
private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
{
//此次2012-02-05修改過=================
if (maxWidth <= 0)
maxWidth = width;
if (maxHeight <= 0)
maxHeight = height;
//以上2012-02-05修改過=================
decimal MAX_WIDTH = (decimal)maxWidth;
decimal MAX_HEIGHT = (decimal)maxHeight;
decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
int newWidth, newHeight;
decimal originalWidth = (decimal)width;
decimal originalHeight = (decimal)height;
if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
{
decimal factor;
// determine the largest factor
if (originalWidth / originalHeight > ASPECT_RATIO)
{
factor = originalWidth / MAX_WIDTH;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
else
{
factor = originalHeight / MAX_HEIGHT;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
}
else
{
newWidth = width;
newHeight = height;
}
return new Size(newWidth, newHeight);
}
/// <summary>
/// 得到圖片格式
/// </summary>
/// <param name="name">文件名稱</param>
/// <returns></returns>
public static ImageFormat GetFormat(string name)
{
string ext = name.Substring(name.LastIndexOf(".") + 1);
switch (ext.ToLower())
{
case "jpg":
case "jpeg":
return ImageFormat.Jpeg;
case "bmp":
return ImageFormat.Bmp;
case "png":
return ImageFormat.Png;
case "gif":
return ImageFormat.Gif;
default:
return ImageFormat.Jpeg;
}
}
/// <summary>
/// 制作小正方形
/// </summary>
/// <param name="image">圖片對象</param>
/// <param name="newFileName">新地址</param>
/// <param name="newSize">長度或?qū)挾?lt;/param>
public static void MakeSquareImage(Image image, string newFileName, int newSize)
{
int i = 0;
int width = image.Width;
int height = image.Height;
if (width > height)
i = height;
else
i = width;
Bitmap b = new Bitmap(newSize, newSize);
try
{
Graphics g = Graphics.FromImage(b);
//設置高質(zhì)量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//設置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//清除整個繪圖面并以透明背景色填充
g.Clear(Color.Transparent);
if (width < height)
g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel);
else
g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel);
SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));
}
finally
{
image.Dispose();
b.Dispose();
}
}
/// <summary>
/// 制作小正方形
/// </summary>
/// <param name="fileName">圖片文件名</param>
/// <param name="newFileName">新地址</param>
/// <param name="newSize">長度或?qū)挾?lt;/param>
public static void MakeSquareImage(string fileName,string newFileName, int newSize)
{
MakeSquareImage(Image.FromFile(fileName), newFileName, newSize);
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C# 運用params修飾符來實現(xiàn)變長參數(shù)傳遞的方法
一般來說,參數(shù)個數(shù)都是固定的,定義為集群類型的參數(shù)可以實現(xiàn)可變數(shù)目參數(shù)的目的,但是.NET提供了更靈活的機制來實現(xiàn)可變數(shù)目參數(shù),這就是使用params修飾符2013-09-09
C#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)
DSL領域?qū)S谜Z言是描述特定領域問題的語言,聽起來很唬人,其實不是什么高深的東西,下面通過實例代碼介紹下C#?使用Fluent?API?創(chuàng)建自己的DSL,感興趣的朋友參考下吧2021-12-12
淺析C#中StringBuilder類的高效及與String的對比
StringBuilder類所創(chuàng)造出來的字符串對象在拼接操作等方面比普通的string類往往要高效很多,這是它們在內(nèi)存劃分方式上的不同所決定的,下面就來淺析C#中StringBuilder類的高效及與String的對比2016-05-05
C#實現(xiàn)的三種模擬自動登錄和提交POST信息的方法
這篇文章主要介紹了C#實現(xiàn)的三種模擬自動登錄和提交POST信息的方法,分別列舉了WebBrowser、WebClient及HttpWebRequest實現(xiàn)自動登錄及提交POST的相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

