.NET基础示例系列之二十:对图片的几种简单处理
生活随笔
收集整理的這篇文章主要介紹了
.NET基础示例系列之二十:对图片的几种简单处理
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
又有一段時(shí)間沒(méi)有更新了,缺少學(xué)習(xí)的熱情了。今天貼幾個(gè)圖片處理的小技巧,希望對(duì)大家有用:
(1)如何獲取.gif圖片中的各個(gè)幀?
(2)如何獲取圖片的縮略圖?
(3)如何“截取”圖片的指定區(qū)域?
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]);
????????//獲取幀數(shù)(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">目標(biāo)圖片開(kāi)始繪制處的坐標(biāo)X值(通常為)</param>
????///?<param?name="pPartStartPointY">目標(biāo)圖片開(kāi)始繪制處的坐標(biāo)Y值(通常為)</param>
????///?<param?name="pPartWidth">目標(biāo)圖片的寬度</param>
????///?<param?name="pPartHeight">目標(biāo)圖片的高度</param>
????///?<param?name="pOrigStartPointX">原始圖片開(kāi)始截取處的坐標(biāo)X值</param>
????///?<param?name="pOrigStartPointY">原始圖片開(kāi)始截取處的坐標(biāo)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));//目標(biāo)位置
????????Rectangle?origRect?=?new?Rectangle(new?Point(pOrigStartPointX,?pOrigStartPointY),?new?Size(pPartWidth,?pPartHeight));//原圖位置(默認(rèn)從原圖中截取的圖片大小等于目標(biāo)圖片的大小)
????????graphics.DrawImage(originalImg,?destRect,origRect,GraphicsUnit.Pixel);
????????partImg.Save(pSavedPath?+?"\\part.jpg",?ImageFormat.Jpeg);
????}
????public?bool?ThumbnailCallback()
????{
????????return?false;
????}
}
?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的.NET基础示例系列之二十:对图片的几种简单处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 孔子学琴
- 下一篇: 使用VMware Server迁移证券公