C# .net 對圖片操作

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class ImageHelper
{
/// <summary>
    /// 獲取圖片中的各幀
    /// </summary>
    /// <param name="pPath">圖片路徑</param>
    /// <param name="pSavePath">保存路徑</param>
    public void GetFrames(string pPath, string pSavedPath)
{
Image gif = Image.FromFile(pPath);
FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
//獲取幀數(gif圖片可能包含多幀,其它格式圖片一般僅一幀)
        int count = gif.GetFrameCount(fd);
//以Jpeg格式保存各幀
        for (int i = 0; i < count; i++)
{
gif.SelectActiveFrame(fd, i);
gif.Save(pSavedPath + "//frame_" + i + ".jpg", ImageFormat.Jpeg);
}
}
/// <summary>
    /// 獲取圖片縮略圖
    /// </summary>
    /// <param name="pPath">圖片路徑</param>
    /// <param name="pSavePath">保存路徑</param>
    /// <param name="pWidth">縮略圖寬度</param>
    /// <param name="pHeight">縮略圖高度</param>
    /// <param name="pFormat">保存格式,通常可以是jpeg</param>
    public void GetSmaller(string pPath, string pSavedPath, int pWidth, int pHeight)
{
try
{
Image smallerImg;
Image originalImg = Image.FromFile(pPath);
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
smallerImg = originalImg.GetThumbnailImage(pWidth, pHeight, callback, IntPtr.Zero);
smallerImg.Save(pSavedPath + "//smaller.jpg", ImageFormat.Jpeg);
}
catch (Exception x)
{
//
        }
}
/// <summary>
    /// 獲取圖片指定部分
    /// </summary>
    /// <param name="pPath">圖片路徑</param>
    /// <param name="pSavePath">保存路徑</param>
    /// <param name="pPartStartPointX">目標圖片開始繪制處的坐標X值(通常為)</param>
    /// <param name="pPartStartPointY">目標圖片開始繪制處的坐標Y值(通常為)</param>
    /// <param name="pPartWidth">目標圖片的寬度</param>
    /// <param name="pPartHeight">目標圖片的高度</param>
    /// <param name="pOrigStartPointX">原始圖片開始截取處的坐標X值</param>
    /// <param name="pOrigStartPointY">原始圖片開始截取處的坐標Y值</param>
    /// <param name="pFormat">保存格式,通常可以是jpeg</param>
    public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
{
Image originalImg = Image.FromFile(pPath);
Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
Graphics graphics = Graphics.FromImage(partImg);
Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目標位置
        Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原圖位置(默認從原圖中截取的圖片大小等于目標圖片的大小)

graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
partImg.Save(pSavedPath + "//part.jpg", ImageFormat.Jpeg);
}
public bool ThumbnailCallback()
{
return false;
}
}

?//********************************************************************************************C#對圖片的幾種簡單處理

#region 生成縮略圖
??????? public void MakeSmallImg(string filePath, string saveImg)
??????? {
??????????? //從文件取得圖片對象
??????????? System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true);

???????????

??????????? //取得圖片大小
??????????? System.Drawing.Size size = new System.Drawing.Size((int)image.Width, (int)image.Height);
??????????? //新建一個bmp圖片
??????????? System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
??????????? //新建一個畫板
??????????? System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
??????????? //設置高質量插值法
??????????? g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
??????????? //設置高質量,低速度呈現平滑程度
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
??????????? //清空一下畫布
??????????? g.Clear(System.Drawing.Color.White);
??????????? //在指定位置畫圖
??????????? g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
??????????????? new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
??????????????? System.Drawing.GraphicsUnit.Pixel);


??????????? ///文字水印
??????????? //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
??????????? //System.Drawing.Font f=new Font("宋體",10);
??????????? //System.Drawing.Brush b=new SolidBrush(Color.Black);
??????????? //G.DrawString("myohmine",f,b,10,10);
??????????? //G.Dispose();

?

??????????? ///圖片水印
??????????? //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
??????????? //Graphics a = Graphics.FromImage(bitmap);
??????????? //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

??????????? //copyImage.Dispose();
??????????? //a.Dispose();
??????????? //copyImage.Dispose();


??????????? //保存高清晰度的縮略圖
??????????? //???bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
??????????? //???加個a表示是縮略圖
??????????? bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);
??????????? g.Dispose();
??????????? image.Dispose();
??????????? bitmap.Dispose();
??????? }
??????? #endregion

//****************************************************************************************關于圖片處理方法(C#)

處理圖片時常用的過程是:讀入圖片文件并轉化為Bitmap -> 處理此Bitmap的每個點以得到需要的效果 -> 保存新的Bitmap到文件
使用C#很方便的就可以把多種格式的圖片文件讀到Bitmap對象中。一句話就夠了,常見的格式都支持,諸如JPEG,BMP,PNG等等。

Bitmap bmp = new Bitmap("文件名");

然后就是怎么處理這個圖片的問題了,與本案無關,pass。

最后就是保存。JPEG雖然是有損壓縮方案,但是它在縮減文件體積和盡可能好的保留原有信息的矛盾上很好的找到了平衡點,所以在很多情況下成為首選的保存方案。

C#當然不會無視這一點,Bitmap類提供了默認的另存為JPEG的方法:

bmp.Save("輸出文件", System.Drawing.Imaging.ImageFormat.Jpeg);

這樣當然很方便,但有時候更在乎文件體積而有時候更在乎圖像質量,是不是有什么辦法可以讓自己來控制壓縮質量呢?

答案是肯定的,bmp.Save方法中有個重載用到了EncoderParameters參數。我們可以在這個參數中加入自己的控制質量。


????????
///?<summary>
????????
///?保存JPG時用
????????
///?</summary>
????????
///?<param?name="mimeType"></param>
????????
///?<returns>得到指定mimeType的ImageCodecInfo</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>
????????
///?保存為JPEG格式,支持壓縮質量選項
????????
///?</summary>
????????
///?<param?name="bmp"></param>
????????
///?<param?name="FileName"></param>
????????
///?<param?name="Qty"></param>
????????
///?<returns></returns>

????????public?static?bool?KiSaveAsJPEG(Bitmap?bmp,?string?FileName,?int?Qty)
????????
{
????????????
try
????????????
{
????????????????EncoderParameter?p;
????????????????EncoderParameters?ps;

????????????????ps?
=?new?EncoderParameters(1);

????????????????p?
=?new?EncoderParameter(System.Drawing.Imaging.Encoder.Quality,?Qty);
????????????????ps.Param[
0]?=?p;

????????????????bmp.Save(FileName,?GetCodecInfo(
"image/jpeg"),?ps);
????????????????
????????????????
return?true;
????????????}

????????????
catch
????????????
{
????????????????
return?false;
????????????}


????????}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389308.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389308.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389308.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據類型之Integer與int

數據類型之Integer與int Java入門 基本數據類型 眾所周知&#xff0c;Java是面向對象的語言&#xff0c;一切皆對象。但是為了兼容人類根深蒂固的數據處理習慣&#xff0c;加快常規數據的處理速度&#xff0c;提供了9種基本數據類型&#xff0c;他們都不具備對象的特性&#xf…

PCA(主成分分析)思想及實現

PCA的概念&#xff1a; PCA是用來實現特征提取的。 特征提取的主要目的是為了排除信息量小的特征&#xff0c;減少計算量等。 簡單來說&#xff1a; 當數據含有多個特征的時候&#xff0c;選取主要的特征&#xff0c;排除次要特征或者不重要的特征。 比如說&#xff1a;我們要…

【安富萊二代示波器教程】第8章 示波器設計—測量功能

第8章 示波器設計—測量功能 二代示波器測量功能實現比較簡單&#xff0c;使用2D函數繪制即可。不過也專門開辟一個章節&#xff0c;為大家做一個簡單的說明&#xff0c;方便理解。 8.1 水平測量功能 8.2 垂直測量功能 8.3 總結 8.1 水平測量功能 水平測量方…

深度學習數據更換背景_開始學習數據科學的最佳方法是了解其背景

深度學習數據更換背景數據科學教育 (DATA SCIENCE EDUCATION) 目錄 (Table of Contents) The Importance of Context Knowledge 情境知識的重要性 (Optional) Research Supporting Context-Based Learning (可選)研究支持基于上下文的學習 The Context of Data Science 數據科學…

熊貓數據集_用熊貓掌握數據聚合

熊貓數據集Data aggregation is the process of gathering data and expressing it in a summary form. This typically corresponds to summary statistics for numerical and categorical variables in a data set. In this post we will discuss how to aggregate data usin…

IOS CALayer的屬性和使用

一、CALayer的常用屬性 1、propertyCGPoint position; 圖層中心點的位置&#xff0c;類似與UIView的center&#xff1b;用來設置CALayer在父層中的位置&#xff1b;以父層的左上角為原點&#xff08;0&#xff0c;0&#xff09;&#xff1b; 2、 property CGPoint anchorPoint…

GridView詳解

快速預覽&#xff1a;GridView無代碼分頁排序GridView選中&#xff0c;編輯&#xff0c;取消&#xff0c;刪除GridView正反雙向排序GridView和下拉菜單DropDownList結合GridView和CheckBox結合鼠標移到GridView某一行時改變該行的背景色方法一鼠標移到GridView某一行時改變該行…

訪問模型參數,初始化模型參數,共享模型參數方法

一. 訪問模型參數 對于使用Sequential類構造的神經網絡&#xff0c;我們可以通過方括號[]來訪問網絡的任一層。回憶一下上一節中提到的Sequential類與Block類的繼承關系。 對于Sequential實例中含模型參數的層&#xff0c;我們可以通過Block類的params屬性來訪問該層包含的所有…

QZEZ第一屆“飯吉圓”杯程序設計競賽

終于到了飯吉圓杯的開賽&#xff0c;這是EZ我參與的歷史上第一場ACM賽制的題目然而沒有罰時 不過題目很好&#xff0c;舉辦地也很成功&#xff0c;為法老點贊&#xff01;&#xff01;&#xff01; 這次和翰爺&#xff0c;吳駿達 dalao&#xff0c;陳樂揚dalao組的隊&#xff0…

談談數據分析 caoz_讓我們談談開放數據…

談談數據分析 caozAccording to the International Open Data Charter(1), it defines open data as those digital data that are made available with the technical and legal characteristics necessary so that they can be freely used, reused and redistributed by any…

數據創造價值_展示數據并創造價值

數據創造價值To create the maximum value, urgency, and leverage in a data partnership, you must present the data available for sale or partnership in a clear and comprehensive way. Partnerships are based upon the concept that you are offering value for valu…

Java入門系列-22-IO流

File類的使用 Java程序如何訪問文件&#xff1f;通過 java.io.File 類 使用File類需要先創建文件對象 File filenew File(String pathname);&#xff0c;創建時在構造函數中指定物理文件或目錄&#xff0c;然后通過文件對象的方法操作文件或目錄的屬性。 \ 是特殊字符&#xff…

缺了一部分

學Java好多年&#xff0c;也參與一次完整項目&#xff0c;覺得讓自己寫項目寫不出來&#xff0c;總覺得缺了一部分。 在這方面愚笨&#xff0c;不知道缺在哪里。以前覺得是知識不夠牢固&#xff0c;于是重復去學&#xff0c;發現就那些東西。如果沒有業務來熟悉的話&#xff0c…

卷積神經網絡——各種網絡的簡潔介紹和實現

各種網絡模型&#xff1a;來源《動手學深度學習》 一&#xff0c;卷積神經網絡&#xff08;LeNet&#xff09; LeNet分為卷積層塊和全連接層塊兩個部分。下面我們分別介紹這兩個模塊。 卷積層塊里的基本單位是卷積層后接最大池化層&#xff1a;卷積層用來識別圖像里的空間模…

數據中臺是下一代大數據_全棧數據科學:下一代數據科學家群體

數據中臺是下一代大數據重點 (Top highlight)Data science has been an eye-catching field for many years now to young individuals having formal education with a bachelors, masters or Ph.D. in computer science, statistics, business analytics, engineering manage…

net如何判斷瀏覽器的類別

回復&#xff1a;.net如何判斷瀏覽器的類別?瀏覽器型號&#xff1a;Request.Browser.Type 瀏覽器名稱&#xff1a;Request.Browser.browser 瀏覽器版本&#xff1a;Request.Browser.Version 瀏覽器Cookie&#xff1a;Request.Browser.Cookies 你的操作系統&#xff1a;Request…

AVS 端能力模塊

Mark 轉載于:https://www.cnblogs.com/clxye/p/9939333.html

pwn學習之四

本來以為應該能出一兩道ctf的pwn了&#xff0c;結果又被sctf打擊了一波。 bufoverflow_a 做這題時libc和堆地址都泄露完成了&#xff0c;卡在了unsorted bin attack上&#xff0c;由于delete會清0變量導致無法寫&#xff0c;一直沒構造出unsorted bin attack&#xff0c;后面根…

優化算法的簡潔實現

動量法 思想&#xff1a; 動量法使用了指數加權移動平均的思想。它將過去時間步的梯度做了加權平均&#xff0c;且權重按時間步指數衰減。 代碼&#xff1a; 在Gluon中&#xff0c;只需要在Trainer實例中通過momentum來指定動量超參數即可使用動量法。 d2l.train_gluon_ch7…

北方工業大學gpa計算_北方大學聯盟倉庫的探索性分析

北方工業大學gpa計算This is my firts publication here and i will start simple.這是我的第一篇出版物&#xff0c;這里我將簡單介紹 。 I want to make an exploratory data analysis of UFRN’s warehouse and answer some questions about the data using Python and Pow…