C#實(shí)現(xiàn)繪制浮雕圖片效果實(shí)例
本文采用C#實(shí)例講解了處理圖片為浮雕效果的實(shí)現(xiàn)方法,這在PS中是一個(gè)常見的功能,也是C#中的一個(gè)簡(jiǎn)單的圖像處理例子。程序先讀取原圖,然后依次訪問每個(gè)像素的RGB值,獲取相鄰兩個(gè)像素的R、G、B值,計(jì)算與左上角像素的RGB分量之差,將計(jì)算后的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è)計(jì)器生成的代碼
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)行圖片的浮雕處理
//依次訪問每個(gè)像素的RGB值
for(int i=Width-1; i>0;i--)
{
for( int j=Height-1; j>0;j--)
{
//獲取相鄰兩個(gè)像素的R、G、B值
color =image.GetPixel(i, j);
colorLeft=image.GetPixel(i-1, j-1);
//計(jì)算與左上角像素的RGB分量之差
//67:控制圖片的最低灰度,128:常量,更改這兩個(gè)值會(huì)得到不同的效果
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);
//將計(jì)算后的RGB值回寫到位圖
image.SetPixel(i, j,colorResult);
}
//繪制浮雕圖
graphics.DrawImage(
image, new Rectangle(Width+10, 0, Width, Height));
}
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
感興趣的朋友可以點(diǎn)此本站下載完整實(shí)例代碼。
相關(guān)文章
unity scrollRect實(shí)現(xiàn)按頁(yè)碼翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了unity scrollRect實(shí)現(xiàn)按頁(yè)碼翻頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#實(shí)現(xiàn)給Word每一頁(yè)設(shè)置不同文字水印的方法詳解
Word中設(shè)置水印時(shí),可使用預(yù)設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時(shí),會(huì)對(duì)所有頁(yè)面都設(shè)置成統(tǒng)一效果。本文以C#?代碼為例,對(duì)Word每一頁(yè)設(shè)置不同的文字水印效果作詳細(xì)介紹,感興趣的可以了解一下2022-07-07
分享我在工作中遇到的多線程下導(dǎo)致RCW無法釋放的問題
最近在做項(xiàng)目中遇到一個(gè)問題,在調(diào)用一個(gè)類庫(kù)中的方法時(shí),出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動(dòng)線程或其他線程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會(huì)導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12
C# Color.FromArgb()及系統(tǒng)顏色對(duì)照表一覽
這篇文章主要介紹了C# Color.FromArgb()及系統(tǒng)顏色對(duì)照表一覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C# 輸出字符串到文本文件中的實(shí)現(xiàn)代碼
本文通過一個(gè)簡(jiǎn)單的代碼給大家介紹C# 輸出字符串到文本文件中,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05

