C#实现绘制浮雕图片效果 C#实现绘制浮雕图片效果实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#实现绘制浮雕图片效果 C#实现绘制浮雕图片效果实例

  2021-03-19 我要评论
想了解C#实现绘制浮雕图片效果实例的相关内容吗,在本文为您仔细讲解C#实现绘制浮雕图片效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,绘制,浮雕图片,下面大家一起来学习吧。

本文采用C#实例讲解了处理图片为浮雕效果的实现方法,这在PS中是一个常见的功能,也是C#中的一个简单的图像处理例子。程序先读取原图,然后依次访问每个像素的RGB值,获取相邻两个像素的R、G、B值,计算与左上角像素的RGB分量之差,将计算后的RGB值回写到位图,最后进行图片的浮雕处理。

主要代码如下:

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 窗体设计器生成的代码
 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:进行雕刻处理
  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;
  //进行图片的浮雕处理
  //依次访问每个像素的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());
 }
 }
}

感兴趣的朋友可以点此本站下载完整实例代码。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们