日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#图片编辑类

發(fā)布時間:2024/1/18 C# 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#图片编辑类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using ?System;
using ?System.Drawing;
using ?System.IO;

/**/ /// ? <summary>
/// ?ImgEdit?的摘要說 明
/// ? </summary>
public ? class ?ImgEdit
{
????
public ?ImgEdit()
????{
????????
//
????????
// ?TODO:?在此處添加構(gòu)造函數(shù)邏輯
????????
//
????}

????
/**/ /// ? <summary>
????
/// ?圖片水 印
????
/// ? </summary>
????
/// ? <param?name="ImgFile"> 原圖文件地址 </param>
????
/// ? <param?name="WaterImg"> 水印圖片地址 </param>
????
/// ? <param?name="sImgPath"> 水印圖片保存地址 </param>
????
/// ? <param?name="Alpha"> 水印透明度設置 </param>
????
/// ? <param?name="iScale"> 水印圖片在原圖上的顯示比例(水平) </param>
????
/// ? <param?name="intDistance"> 水印圖片在原圖上的邊距確定,以圖片的右邊和下邊為準,當設定的邊距超過一定大小后參 數(shù)會自動失效 </param>
???? public ? bool ?ImgWater( string ?ImgFile,? string ?WaterImg,? string ?sImgPath,? float ?Alpha,? float ?iScale,? int ?intDistance)
????{
????????
try
????????{
????????????
// 裝載圖片
????????????FileStream?fs? = ? new ?FileStream(ImgFile,?FileMode.Open);
????????????BinaryReader?br?
= ? new ?BinaryReader(fs);
????????????
byte []?bytes? = ?br.ReadBytes(( int )fs.Length);
????????????br.Close();
????????????fs.Close();
????????????MemoryStream?ms?
= ? new ?MemoryStream(bytes);

????????????System.Drawing.Image?imgPhoto?
= ?System.Drawing.Image.FromStream(ms);
????????????
int ?imgPhotoWidth? = ?imgPhoto.Width;
????????????
int ?imgPhotoHeight? = ?imgPhoto.Height;

????????????System.Drawing.Image?imgWatermark?
= ? new ?Bitmap(WaterImg);
????????????
int ?imgWatermarkWidth? = ?imgWatermark.Width;
????????????
int ?imgWatermarkHeight? = ?imgWatermark.Height;


????????????
// 計算水印圖片尺寸
???????????? decimal ?aScale? = ?Convert.ToDecimal(iScale);
????????????
decimal ?pScale? = ? 0.05M ;
????????????
decimal ?MinScale? = ?aScale? - ?pScale;
????????????
decimal ?MaxScale? = ?aScale? + ?pScale;

????????????
// 設置比例
???????????? int ?imgWatermarkWidthNew? = ?Convert.ToInt32(imgPhotoWidth? * ?aScale);
????????????
int ?imgWatermarkHeightNew? = ?Convert.ToInt32((imgPhotoWidth? * ?aScale? / ?imgWatermarkWidth)? * ?imgWatermarkHeight);


????????????
// 將原圖畫出來
????????????Bitmap?bmPhoto? = ? new ?Bitmap(imgPhotoWidth,?imgPhotoHeight,?System.Drawing.Imaging.PixelFormat.Format24bppRgb);
????????????bmPhoto.SetResolution(
72 ,? 72 );
????????????Graphics?gbmPhoto?
= ?Graphics.FromImage(bmPhoto);

????????????gbmPhoto.InterpolationMode?
= ?System.Drawing.Drawing2D.InterpolationMode.High;
????????????gbmPhoto.SmoothingMode?
= ?System.Drawing.Drawing2D.SmoothingMode.HighQuality;
????????????gbmPhoto.Clear(Color.White);
????????????gbmPhoto.DrawImage(imgPhoto,?
new ?Rectangle( 0 ,? 0 ,?imgPhotoWidth,?imgPhotoHeight),? 0 ,? 0 ,?imgPhotoWidth,?imgPhotoHeight,?GraphicsUnit.Pixel);

????????????Bitmap?bmWatermark?
= ? new ?Bitmap(bmPhoto);
????????????bmWatermark.SetResolution(imgPhoto.HorizontalResolution,?imgPhoto.VerticalResolution);

????????????Graphics?gWatermark?
= ?Graphics.FromImage(bmWatermark);

????????????
// 指定高質(zhì)量顯示水印圖片質(zhì)量
????????????gWatermark.InterpolationMode? = ?System.Drawing.Drawing2D.InterpolationMode.High;
????????????gWatermark.SmoothingMode?
= ?System.Drawing.Drawing2D.SmoothingMode.HighQuality;
????????????System.Drawing.Imaging.ImageAttributes?imageAttributes?
= ? new ?System.Drawing.Imaging.ImageAttributes();

????????????
// 設置兩種顏色,達到合成效果
????????????System.Drawing.Imaging.ColorMap?colorMap? = ? new ?System.Drawing.Imaging.ColorMap();
????????????colorMap.OldColor?
= ?Color.FromArgb( 255 ,? 0 ,? 255 ,? 0 );
????????????colorMap.NewColor?
= ?Color.FromArgb( 0 ,? 0 ,? 0 ,? 0 );

????????????System.Drawing.Imaging.ColorMap[]?remapTable?
= ?{?colorMap?};

????????????imageAttributes.SetRemapTable(remapTable,?System.Drawing.Imaging.ColorAdjustType.Bitmap);

????????????
// 用矩陣設置水印圖片透明度
???????????? float [][]?colorMatrixElements? = ?{?
???????????????
new ? float []?{ 1.0f ,?? 0.0f ,?? 0.0f ,?? 0.0f ,? 0.0f },
???????????????
new ? float []?{ 0.0f ,?? 1.0f ,?? 0.0f ,?? 0.0f ,? 0.0f },
???????????????
new ? float []?{ 0.0f ,?? 0.0f ,?? 1.0f ,?? 0.0f ,? 0.0f },
???????????????
new ? float []?{ 0.0f ,?? 0.0f ,?? 0.0f ,??Alpha,? 0.0f },
???????????????
new ? float []?{ 0.0f ,?? 0.0f ,?? 0.0f ,?? 0.0f ,? 1.0f }
????????????};

????????????System.Drawing.Imaging.ColorMatrix?wmColorMatrix?
= ? new ?System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
????????????imageAttributes.SetColorMatrix(wmColorMatrix,?System.Drawing.Imaging.ColorMatrixFlag.Default,?System.Drawing.Imaging.ColorAdjustType.Bitmap);

????????????
// 確定水印邊距
???????????? int ?xPos? = ?imgPhotoWidth? - ?imgWatermarkWidthNew;
????????????
int ?yPos? = ?imgPhotoHeight? - ?imgWatermarkHeightNew;
????????????
int ?xPosOfWm? = ? 0 ;
????????????
int ?yPosOfWm? = ? 0 ;

????????????
if ?(xPos? > ?intDistance)
????????????????xPosOfWm?
= ?xPos? - ?intDistance;
????????????
else
????????????????xPosOfWm?
= ?xPos;

????????????
if ?(yPos? > ?intDistance)
????????????????yPosOfWm?
= ?yPos? - ?intDistance;
????????????
else
????????????????yPosOfWm?
= ?yPos;

????????????gWatermark.DrawImage(imgWatermark,?
new ?Rectangle(xPosOfWm,?yPosOfWm,?imgWatermarkWidthNew,?imgWatermarkHeightNew),? 0 ,? 0 ,?imgWatermarkWidth,?imgWatermarkHeight,?GraphicsUnit.Pixel,?imageAttributes);

????????????imgPhoto?
= ?bmWatermark;


????????????imgPhoto.Save(sImgPath,?System.Drawing.Imaging.ImageFormat.Jpeg);

????????????
// 銷毀對象
????????????gbmPhoto.Dispose();
????????????gWatermark.Dispose();

????????????imgPhoto.Dispose();
????????????imgWatermark.Dispose();

????????????
return ? true ;
????????}
????????
catch ?(Exception?ex)
????????{
????????????
return ? false ;
????????}
????}



????
/**/ /// ? <summary>
????
/// ?文字水 印
????
/// ? </summary>
????
/// ? <param?name="ImgFile"> 原圖文件地址 </param>
????
/// ? <param?name="TextFont"> 水印文字 </param>
????
/// ? <param?name="sImgPath"> 文字水印圖片保存地址 </param>
????
/// ? <param?name="Alpha"> 文字透明度?其數(shù)值的范圍在0到255 </param>
????
/// ? <param?name="widthFont"> 文字塊在圖片中所占寬度比例 </param>
????
/// ? <param?name="hScale"> 高度位置 </param>
???? public ? bool ?TextWater( string ?ImgFile,? string ?TextFont,? string ?sImgPath,? int ?Alpha,? float ?widthFont,? float ?hScale)
????{
????????
try
????????{
????????????FileStream?fs?
= ? new ?FileStream(ImgFile,?FileMode.Open);
????????????BinaryReader?br?
= ? new ?BinaryReader(fs);
????????????
byte []?bytes? = ?br.ReadBytes(( int )fs.Length);
????????????br.Close();
????????????fs.Close();
????????????MemoryStream?ms?
= ? new ?MemoryStream(bytes);

????????????System.Drawing.Image?imgPhoto?
= ?System.Drawing.Image.FromStream(ms);
????????????
int ?imgPhotoWidth? = ?imgPhoto.Width;
????????????
int ?imgPhotoHeight? = ?imgPhoto.Height;

????????????Bitmap?bmPhoto?
= ? new ?Bitmap(imgPhotoWidth,?imgPhotoHeight,?System.Drawing.Imaging.PixelFormat.Format24bppRgb);
????????????bmPhoto.SetResolution(
72 ,? 72 );
????????????Graphics?gbmPhoto?
= ?Graphics.FromImage(bmPhoto);
????????????gbmPhoto.Clear(Color.FromName(
" white " )); // gif背景色
????????????gbmPhoto.InterpolationMode? = ?System.Drawing.Drawing2D.InterpolationMode.High;
????????????gbmPhoto.SmoothingMode?
= ?System.Drawing.Drawing2D.SmoothingMode.HighQuality;
????????????gbmPhoto.DrawImage(imgPhoto,?
new ?Rectangle( 0 ,? 0 ,?imgPhotoWidth,?imgPhotoHeight),? 0 ,? 0 ,?imgPhotoWidth,?imgPhotoHeight,?GraphicsUnit.Pixel);

????????????
// 建立字體大小的數(shù)組,循環(huán)找出適合圖片的水印字體
???????????? int []?sizes? = ? new ? int []?{? 1000 ,? 800 ,? 700 ,? 650 ,? 600 ,? 560 ,? 540 ,? 500 ,? 450 ,? 400 ,? 380 ,? 360 ,? 340 ,? 320 ,? 300 ,? 280 ,? 260 ,? 240 ,? 220 ,? 200 ,? 180 ,? 160 ,? 140 ,? 120 ,? 100 ,? 80 ,? 72 ,? 64 ,? 48 ,? 32 ,? 28 ,? 26 ,? 24 ,? 20 ,? 28 ,? 16 ,? 14 ,? 12 ,? 10 ,? 8 ,? 6 ,? 4 ,? 2 ?};
????????????System.Drawing.Font?crFont?
= ? null ;
????????????System.Drawing.SizeF?crSize?
= ? new ?SizeF();
????????????
for ?( int ?i? = ? 0 ;?i? < ? 43 ;?i ++ )
????????????{
????????????????crFont?
= ? new ?Font( " arial " ,?sizes[i],?FontStyle.Bold);
????????????????crSize?
= ?gbmPhoto.MeasureString(TextFont,?crFont);

????????????????
if ?(( ushort )crSize.Width? < ?( ushort )imgPhotoWidth? * ?widthFont)
????????????????????
break ;
????????????}

????????????
// 設置水印字體的位 置
???????????? int ?yPixlesFromBottom? = ?( int )(imgPhotoHeight? * ?hScale);
????????????
float ?yPosFromBottom? = ?((imgPhotoHeight? - ?yPixlesFromBottom)? - ?(crSize.Height? / ? 2 ));
????????????
float ?xCenterOfImg? = ?(imgPhotoWidth? * ? 1 ? / ? 2 );

????????????System.Drawing.StringFormat?StrFormat?
= ? new ?System.Drawing.StringFormat();
????????????StrFormat.Alignment?
= ?System.Drawing.StringAlignment.Center;

????????????
// 畫兩次制造透明效果
????????????System.Drawing.SolidBrush?semiTransBrush2? = ? new ?System.Drawing.SolidBrush(Color.FromArgb(Alpha,? 0 ,? 0 ,? 0 ));
????????????gbmPhoto.DrawString(TextFont,?crFont,?semiTransBrush2,?
new ?System.Drawing.PointF(xCenterOfImg? + ? 1 ,?yPosFromBottom? + ? 1 ),?StrFormat);

????????????System.Drawing.SolidBrush?semiTransBrush?
= ? new ?System.Drawing.SolidBrush(Color.FromArgb(Alpha,? 255 ,? 255 ,? 255 ));
????????????gbmPhoto.DrawString(TextFont,?crFont,?semiTransBrush,?
new ?System.Drawing.PointF(xCenterOfImg,?yPosFromBottom),?StrFormat);

????????????bmPhoto.Save(sImgPath,?System.Drawing.Imaging.ImageFormat.Jpeg);

????????????gbmPhoto.Dispose();
????????????imgPhoto.Dispose();
????????????bmPhoto.Dispose();

????????????
return ? true ;
????????}
????????
catch ?(Exception?ex)
????????{
????????????
return ? false ;
????????}
????}


????
/**/ /// ? <summary>
????
/// ?縮略圖
????
/// ? </summary>
????
/// ? <param?name="ImgFile"> 原圖文件地址 </param>
????
/// ? <param?name="sImgPath"> 縮略圖保存地址 </param>
????
/// ? <param?name="ResizeWidth"> 縮略圖寬度 </param>
????
/// ? <param?name="ResizeHeight"> 縮略圖高度 </param>
????
/// ? <param?name="BgColor"> 縮略圖背景顏色,注意,背景顏色只能指定KnownColor中的值,如 white,blue,red,green等 </param>
???? public ? bool ?ResizeImg( string ?ImgFile,? string ?sImgPath,? int ?ResizeWidth,? int ?ResizeHeight,? string ?BgColor)
????{
????????
try
????????{
????????????FileStream?fs?
= ? new ?FileStream(ImgFile,?FileMode.Open);
????????????BinaryReader?br?
= ? new ?BinaryReader(fs);
????????????
byte []?bytes? = ?br.ReadBytes(( int )fs.Length);
????????????br.Close();
????????????fs.Close();
????????????MemoryStream?ms?
= ? new ?MemoryStream(bytes);

????????????System.Drawing.Image?imgPhoto?
= ?System.Drawing.Image.FromStream(ms);
????????????
int ?imgPhotoWidth? = ?imgPhoto.Width;
????????????
int ?imgPhotoHeight? = ?imgPhoto.Height;
????????????
int ?StartX? = ? 0 ;
????????????
int ?StartY? = ? 0 ;
????????????
int ?NewWidth? = ?imgPhotoWidth;
????????????
int ?NewHeight? = ?imgPhotoHeight;

????????????
// 計算縮放圖片尺寸
???????????? if ?(NewWidth? > ?ResizeWidth)
????????????{
????????????????NewWidth?
= ?ResizeWidth;
????????????????NewHeight?
= ?Convert.ToInt32(imgPhotoHeight? * ?Math.Round(Convert.ToDecimal(NewWidth)? / ?Convert.ToDecimal(imgPhotoWidth),? 10 ));
????????????}

????????????
if ?(NewHeight? > ?ResizeHeight)
????????????{
????????????????NewHeight?
= ?ResizeHeight;
????????????????NewWidth?
= ?Convert.ToInt32(imgPhotoWidth? * ?Math.Round(Convert.ToDecimal(NewHeight)? / ?Convert.ToDecimal(imgPhotoHeight),? 10 ));
????????????}

????????????StartX?
= ?ResizeWidth? - ?NewWidth;
????????????StartY?
= ?ResizeHeight? - ?NewHeight;

????????????StartX?
= ?StartX? > ? 0 ? ? ?StartX? / ? 2 ?:? 0 ;
????????????StartY?
= ?StartY? > ? 0 ? ? ?StartY? / ? 2 ?:? 0 ;

????????????Bitmap?bmPhoto?
= ? new ?Bitmap(ResizeWidth,?ResizeHeight,?System.Drawing.Imaging.PixelFormat.Format24bppRgb);
????????????bmPhoto.SetResolution(
72 ,? 72 );
????????????Graphics?gbmPhoto?
= ?Graphics.FromImage(bmPhoto);
????????????gbmPhoto.Clear(Color.FromName(BgColor));
????????????gbmPhoto.InterpolationMode?
= ?System.Drawing.Drawing2D.InterpolationMode.High;
????????????gbmPhoto.SmoothingMode?
= ?System.Drawing.Drawing2D.SmoothingMode.HighQuality;

????????????gbmPhoto.DrawImage(imgPhoto,?
new ?Rectangle(StartX,?StartY,?NewWidth,?NewHeight),? new ?Rectangle( 0 ,? 0 ,?imgPhotoWidth,?imgPhotoHeight),?GraphicsUnit.Pixel);
????????????bmPhoto.Save(sImgPath,?System.Drawing.Imaging.ImageFormat.Jpeg);

????????????imgPhoto.Dispose();
????????????gbmPhoto.Dispose();
????????????bmPhoto.Dispose();

????????????
return ? true ;
????????}
????????
catch ?(Exception?err)
????????{
????????????
return ? false ;
????????}
????}


????
/**/ /// ? <summary>
????
/// ?圖片剪 切
????
/// ? </summary>
????
/// ? <param?name="ImgFile"> 原圖文件地址 </param>
????
/// ? <param?name="sImgPath"> 縮略圖保存地址 </param>
????
/// ? <param?name="PointX"> 剪切起始點?X坐標 </param>
????
/// ? <param?name="PointY"> 剪切起始點?Y坐標 </param>
????
/// ? <param?name="CutWidth"> 剪切寬度 </param>
????
/// ? <param?name="CutHeight"> 剪切高度 </param>
???? public ? bool ?CutImg( string ?ImgFile,? string ?sImgPath,? int ?PointX,? int ?PointY,? int ?CutWidth,? int ?CutHeight)
????{
????????
try
????????{
????????????FileStream?fs?
= ? new ?FileStream(ImgFile,?FileMode.Open);
????????????BinaryReader?br?
= ? new ?BinaryReader(fs);
????????????
byte []?bytes? = ?br.ReadBytes(( int )fs.Length);
????????????br.Close();
????????????fs.Close();
????????????MemoryStream?ms?
= ? new ?MemoryStream(bytes);
????????????System.Drawing.Image?imgPhoto?
= ?System.Drawing.Image.FromStream(ms);

????????????
// System.Drawing.Image?imgPhoto?=?System.Drawing.Image.FromFile(ImgFile);
????????????
// 此處只能用filestream,用?System.Drawing.Image則會報多過進程訪問文件的錯誤,會鎖定文件
????????????Bitmap?bmPhoto? = ? new ?Bitmap(CutWidth,?CutHeight,?System.Drawing.Imaging.PixelFormat.Format24bppRgb);
????????????bmPhoto.SetResolution(
72 ,? 72 );
????????????Graphics?gbmPhoto?
= ?Graphics.FromImage(bmPhoto);
????????????gbmPhoto.InterpolationMode?
= ?System.Drawing.Drawing2D.InterpolationMode.High;
????????????gbmPhoto.SmoothingMode?
= ?System.Drawing.Drawing2D.SmoothingMode.HighQuality;

????????????gbmPhoto.DrawImage(imgPhoto,?
new ?Rectangle( 0 ,? 0 ,?CutWidth,?CutHeight),? new ?Rectangle(PointX,?PointY,?CutHeight,?CutHeight),?GraphicsUnit.Pixel);
????????????bmPhoto.Save(sImgPath,?System.Drawing.Imaging.ImageFormat.Jpeg);

????????????imgPhoto.Dispose();
????????????gbmPhoto.Dispose();
????????????bmPhoto.Dispose();

????????????
return ? true ;
????????}
????????
catch ?(Exception?err)
????????{
????????????
return ? false ;
????????}
????}
}

總結(jié)

以上是生活随笔為你收集整理的C#图片编辑类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲码视频 | 成年免费视频黄网站在线观看 | 国产精品一区二区免费在线观看 | 国产一区二区精品在线 | 农村黄色片| 性活交片大全免费看 | 无码精品人妻一区二区 | 亚洲精品久久视频 | 成人动漫视频 | 久久国产人妻一区二区免色戒电影 | 最近中文字幕在线中文高清版 | 韩国中文字幕hd久久精品 | 加勒比在线一区 | 国产精品交换 | 美女啪啪无遮挡 | 搞黄视频在线观看 | 夜色导航 | 91中文字幕在线视频 | 一本大道熟女人妻中文字幕在线 | 久久99久久99精品免视看婷婷 | 亚洲精品鲁一鲁一区二区三区 | 免费黄色网址大全 | 大尺度一区二区 | 亚洲美女av网站 | 美女又爽又黄免费视频 | 日本理伦片午夜理伦片 | 欧美色图激情 | 91精品国产综合久久久久久久 | 国产东北露脸精品视频 | 国模小丫大尺度啪啪人体 | 超碰超碰97 | 最近免费中文字幕中文高清百度 | 性色av浪潮av | 亚欧成人精品一区二区 | 日韩午夜在线 | 软萌小仙自慰喷白浆 | 动漫美女舌吻 | 日韩毛片在线免费观看 | 一区二区三区四区视频 | 欧美专区在线播放 | 免费高清视频在线观看 | 热久久久 | 日韩精品2区 | 免费看黄色片视频 | 亚洲第一精品网站 | 99国产精品久久久久99打野战 | 国产三级精品三级 | 伊人久久狼人 | 老鸭窝av在线 | 亚洲手机在线观看 | 亚洲精品一区二区口爆 | 色乱码一区二区三区熟女 | 88av在线播放 | 久久神马影院 | 无码精品a∨在线观看中文 福利片av | 国产三级日本三级在线播放 | 女~淫辱の触手3d动漫 | 影音先锋在线观看视频 | 黄色av网站在线播放 | 日韩大片一区 | 天天操你 | 精品无码一区二区三区电影桃花 | 91麻豆精品国产91久久久更新时间 | 日本三级韩国三级三级a级按摩 | 打屁股调教视频 | 免费a级片视频 | 福利在线一区 | 制服丝袜第二页 | 涩涩亚洲 | 天天激情站| 青青草成人在线 | 波多野结衣一区 | 26uuu精品一区二区在线观看 | 午夜视频在线 | 日韩精品无码一区二区三区久久久 | 日本japanese乳偷乱熟 | 在线中出 | 国产视频精选 | 色综合久久久久综合体桃花网 | 国产成人综合自拍 | 999伊人| 毛片在线视频观看 | 波多野结衣黄色片 | 亚洲天堂五月 | 色久在线 | 手机成人在线视频 | 在线免费黄色网 | av看片| 成人欧美一区二区三区小说 | 美国一区二区三区 | 在线涩涩 | 久久精品综合视频 | 午夜美女福利视频 | 神秘马戏团在线观看免费高清中文 | 91视频日本 | 色七七在线| 亚洲精品乱码久久久久久蜜桃图片 | 无码人妻精品一区二区三区66 | 91插插插插插 |