C#實現(xiàn)繪制浮雕圖片效果實例
本文采用C#實例講解了處理圖片為浮雕效果的實現(xiàn)方法,這在PS中是一個常見的功能,也是C#中的一個簡單的圖像處理例子。程序先讀取原圖,然后依次訪問每個像素的RGB值,獲取相鄰兩個像素的R、G、B值,計算與左上角像素的RGB分量之差,將計算后的RGB值回寫到位圖,最后進(jìn)行圖片的浮雕處理。
主要代碼如下:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; namespace EmbossColander { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗體設(shè)計器生成的代碼 private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(350,200); this.Text = "Form1"; } #endregion protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); Graphics graphics = e.Graphics; graphics.Clear(Color.White); graphics.ScaleTransform(0.7f,0.7f); Bitmap image = new Bitmap("dog.bmp"); int Width = image.Width; int Height = image.Height; //image2:進(jìn)行雕刻處理 Bitmap image2 = image.Clone(new Rectangle(0,0,Width,Height),PixelFormat.DontCare ); //繪制原圖 graphics.DrawImage( image, new Rectangle(0, 0, Width, Height)); Color color, colorTemp,colorLeft; //進(jìn)行圖片的浮雕處理 //依次訪問每個像素的RGB值 for(int i=Width-1; i>0;i--) { for( int j=Height-1; j>0;j--) { //獲取相鄰兩個像素的R、G、B值 color =image.GetPixel(i, j); colorLeft=image.GetPixel(i-1, j-1); //計算與左上角像素的RGB分量之差 //67:控制圖片的最低灰度,128:常量,更改這兩個值會得到不同的效果 int r = Math.Max(67,Math.Min(255, Math.Abs(color.R-colorLeft.R+128))); int g = Math.Max(67,Math.Min(255, Math.Abs(color.G-colorLeft.G+128))); int b = Math.Max(67,Math.Min(255, Math.Abs(color.B-colorLeft.B+128))); Color colorResult=Color.FromArgb(255,r,g,b); //將計算后的RGB值回寫到位圖 image.SetPixel(i, j,colorResult); } //繪制浮雕圖 graphics.DrawImage( image, new Rectangle(Width+10, 0, Width, Height)); } } [STAThread] static void Main() { Application.Run(new Form1()); } } }
感興趣的朋友可以點此本站下載完整實例代碼。
相關(guān)文章
C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實現(xiàn)引用類型轉(zhuǎn)換
這篇文章介紹了C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實現(xiàn)引用類型轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08如何利用Jenkins + TFS為.Net Core實現(xiàn)持續(xù)集成/部署詳解
這篇文章主要給大家介紹了關(guān)于如何利用Jenkins + TFS為.Net Core實現(xiàn)持續(xù)集成/部署的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05c#動態(tài)調(diào)用Webservice的兩種方法實例
這篇文章介紹了c#動態(tài)調(diào)用Webservice的兩種方法實例,有需要的朋友可以參考一下2013-08-08Unity3D UGUI實現(xiàn)縮放循環(huán)拖動卡牌展示效果
這篇文章主要為大家詳細(xì)介紹了Unity3D UGUI實現(xiàn)縮放循環(huán)拖動展示卡牌效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02