目錄
實現垂直百葉窗效果。
原理:將圖像分成若干個區域,各個區月以一種漸進的方式逐漸顯示,效果就像百葉窗翻動一樣。主要用到了Bitmap類的GetRixel和SetPixel方法,獲取和設置圖像中指定像素的顏色,然後使用Refresh方法重新刷新窗體背景。
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp"; //設置文件的類型
openFileDialog1.ShowDialog(); //打開文件對話框
myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName); //根據文件的路徑實例化Image類
this.BackgroundImage = myImage; //顯示打開的圖片
}
private void button2_Click(object sender, EventArgs e)
{
try
{
Bitmap myBitmap = (Bitmap)this.BackgroundImage.Clone(); //用窗體背景的複本實例化Bitmap類
int intWidth = myBitmap.Width; //記錄圖片的寬度
int intHeight = myBitmap.Height / 20; //記錄圖片的指定高度
Graphics myGraphics = this.CreateGraphics(); //創建窗體的Graphics類
myGraphics.Clear(Color.WhiteSmoke); //用指定的顏色清除窗體背景
Point[] myPoint = new Point[30]; //定義數組
for (int i = 0; i < 30; i++) //記錄百葉窗各節點的位置
{
myPoint[i].X = 0;
myPoint[i].Y = i * intHeight;
}
Bitmap bitmap = new Bitmap(myBitmap.Width, myBitmap.Height);//實例化Bitmap類
//通過調用Bitmap對象的SetPixel方法重新設置圖像的像素點顏色,從而實現百葉窗效果
for (int m = 0; m < intHeight; m++)
{
for (int n = 0; n < 20; n++)
{
for (int j = 0; j < intWidth; j++)
{
bitmap.SetPixel(myPoint[n].X + j, myPoint[n].Y + m, myBitmap.GetPixel(myPoint[n].X + j,myPoint[n].Y + m));//獲取當前象素顏色值
}
}
this.Refresh(); //繪製無效
this.BackgroundImage = bitmap; //顯示百葉窗體的效果
System.Threading.Thread.Sleep(100); //線程掛起
}
}
catch { }
}
水平交錯顯示圖像
實現原理:將一幅圖像分成左右兩部分,然後使他們分別從左右兩個方向向窗體中間移動,最終形成一幅圖畫。主要用到了Bitmap的GetPixel方法和SetPixel方法。
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp"; //設置文件的類型
openFileDialog1.ShowDialog();//打開文件對話框
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName); //根據文件的路徑實例化Image類
myBitmap = new Bitmap(myImage); //實例化Bitmap類
this.BackgroundImage = myBitmap; //顯示打開的圖片
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int intWidth = this.BackgroundImage.Width; //獲取背景圖片的寬度
int intHeight = this.BackgroundImage.Height; //獲取背景圖片的高度
Graphics myGraphics = this.CreateGraphics();//創建窗體的Graphics類
myGraphics.Clear(Color.WhiteSmoke); //以指定的顏色清除
Bitmap bitmap = new Bitmap(intWidth, intHeight); //實例化Bitmap類
int i = 0;
//通過調用Bitmap對象的SetPixel方法實現水平交錯效果顯示圖像
while (i <= intWidth / 2)
{
for (int m = 0; m <= intHeight - 1; m++)
{
bitmap.SetPixel(i, m, myBitmap.GetPixel(i, m)); //設置當前象素的顏色值
}
for (int n = 0; n <= intHeight - 1; n++)
{
bitmap.SetPixel(intWidth - i - 1, n, myBitmap.GetPixel(intWidth - i - 1, n));//設置當前象素的顏色值
}
i++;
this.Refresh(); //工作區無效
this.BackgroundImage = bitmap; //顯示水平交錯的圖片
System.Threading.Thread.Sleep(10); //線程掛起
}
}
catch { }
}
紋理效果展示
實現的原理:使用Bitmap對象的LockBits方法將圖像鎖定到內存中,然後通過BitmapData對象的Scan0屬性獲得圖像中第一個像素數據的地址,最後通過使用System.Runtime.InteropServices.Marshal類的Copy方法爲指定圖像的像素點找死,並用變換後的圖像作爲窗體的新背景。
代碼:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp";//設置文件的類型
openFileDialog1.ShowDialog(); //打開文件對話框
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);//根據文件的路徑實例化Image類
myBitmap = new Bitmap(myImage); //實例化Bitmap類
this.BackgroundImage = myBitmap;//顯示打開的圖片
}
private void button2_Click(object sender, EventArgs e)
{
try
{
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);//實例化Image類
myBitmap = new Bitmap(myImage); //實例化Bitmap類
Rectangle rect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height); //實例化Rectangle類
System.Drawing.Imaging.BitmapData bmpData = myBitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite, myBitmap.PixelFormat); //將指定圖像鎖定到內存中
IntPtr ptr = bmpData.Scan0; //獲得圖像中第一個像素數據的地址
int bytes = myBitmap.Width * myBitmap.Height * 3;//設置大小
byte[] rgbValues = new byte[bytes];//實例化byte數組
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); //使用RGB值爲聲明的rgbValues數組賦值
for (int counter = 0; counter < rgbValues.Length; counter += 3) //初始化大小
rgbValues[counter] = 255;
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); //使用RGB值爲圖像的像素點着色
myBitmap.UnlockBits(bmpData); //從內存中解鎖圖像
this.BackgroundImage = myBitmap;//顯示設置後的圖片
}
catch { }
}
實現浮雕效果
實現原理:通過Bitmap對象的GetPixel方法獲取各像素點的顏色,然後分別使用Color對象的RGB屬性獲得各像素點的RGB元素值,並使用這些值減去相鄰像素值再加上128,最後使用Bitmap對象的SetPixel方法重新位圖像的像素點着色。
代碼:
public partial class Frm_Main : Form
{
Bitmap myBitmap;
Image myImage;
public Frm_Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp"; //設置文件的類型
openFileDialog1.ShowDialog(); //打開文件對話框
myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName); //根據文件的路徑實例化Image類
myBitmap = new Bitmap(myImage); //實例化Bitmap類
this.BackgroundImage = myBitmap; //顯示打開的圖片
}
private void button2_Click(object sender, EventArgs e)
{
try
{
myBitmap = new Bitmap(myImage); //實例化Bitmap類
//遍歷圖片中的所有象素
for (int i = 0; i < myBitmap.Width - 1; i++)
{
for (int j = 0; j < myBitmap.Height - 1; j++)
{
Color Color1 = myBitmap.GetPixel(i, j);//獲取當前象素的顏色值
Color Color2 = myBitmap.GetPixel(i + 1, j + 1); //獲取斜點下象素的顏色值
int red = Math.Abs(Color1.R - Color2.R + 128); //設置R色值
int green = Math.Abs(Color1.G - Color2.G + 128); //設置G色值
int blue = Math.Abs(Color1.B - Color2.B + 128); //設置B色值
//顏色處理
if (red > 255) red = 255; //如果R色值大於255,將R色值設爲255
if (red < 0) red = 0; //如果R色值小於0,將R色值設爲0
if (green > 255) green = 255; //如果G色值大於255,將R色值設爲255
if (green < 0) green = 0; //如果G色值小於0,將R色值設爲0
if (blue > 255) blue = 255; //如果B色值大於255,將R色值設爲255
if (blue < 0) blue = 0; //如果B色值小於0,將R色值設爲0
//通過調用Bitmap對象的SetPixel方法爲圖像的像素點重新着色
myBitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
}
}
this.BackgroundImage = myBitmap; //顯示處理後的圖片
}
catch { }
}
}
實現膠片效果
實現原理:對現有的圖像取反色。
代碼:
private void button1_Click(object sender, EventArgs e)
{
try
{
int Height = this.pictureBox1.Image.Height;//獲取圖片高度
int Width = this.pictureBox1.Image.Width;//獲取圖片寬度
Bitmap newbitmap = new Bitmap(Width, Height);//實例化位圖對象
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;//獲取原圖
Color pixel;//定義一個Color結構
//遍歷圖片的每個位置
for (int x = 1; x < Width; x++)
{
for (int y = 1; y < Height; y++)
{
int r, g, b;//定義3個變量,用來記錄指定點的R\G\B值
pixel = oldbitmap.GetPixel(x, y);//獲取指定點的像素值
r = 255 - pixel.R;//記錄R值
g = 255 - pixel.G;//記錄G值
b = 255 - pixel.B;//記錄B值
newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));//爲指定點重新着色
}
}
this.pictureBox1.Image = newbitmap;//顯示底片效果的圖像
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
實現積木效果
實現原理:對圖像中的各個像素點着重着色,取單個像素點的RGB值,取平均值,平均值大於128的設置爲255,否則的設置爲0。
代碼:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp";
openFileDialog1.ShowDialog();
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);
this.BackgroundImage = myImage;
}
private void button2_Click(object sender, EventArgs e)
{
Graphics myGraphics = this.CreateGraphics(); //創建窗體的Graphics類
Bitmap myBitmap = new Bitmap(openFileDialog1.FileName); //實例化Bitmap類
int myWidth, myHeight, i, j, iAvg, iPixel; //定義變量
Color myColor, myNewColor; //定義顏色變量
RectangleF myRect;
myWidth = myBitmap.Width; //獲取背景圖片的寬度
myHeight = myBitmap.Height; //獲取背景圖片的高度
myRect = new RectangleF(0, 0, myWidth, myHeight); //獲取圖片的區域
Bitmap bitmap = myBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare); //實例化Bitmap類
i = 0;
//遍歷圖片的所有象素
while (i < myWidth - 1)
{
j = 0;
while (j < myHeight - 1)
{
myColor = bitmap.GetPixel(i, j); //獲取當前象素的顏色值
iAvg = (myColor.R + myColor.G + myColor.B) / 3; //平均法
iPixel = 0;
if (iAvg >= 128) //如果顏色值大於等於128
iPixel = 0; //設置爲255
else
iPixel = 255;
//通過調用Color對象的FromArgb方法獲得圖像各像素點的顏色
myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);
bitmap.SetPixel(i, j, myNewColor); //設置顏色值
j = j + 1;
}
i = i + 1;
}
myGraphics.Clear(Color.WhiteSmoke);
bitmap.Save("D:11.bmp");//以指定的顏色清除
myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight)); //繪製處理後的圖片
}
柔化效果顯示圖片
實現原理:將當前像素和周圍的像素點的顏色進行比較,如果顏色差距較大,則去平均值,否則取原來的值,重新給你圖像着色。
代碼:
private void button2_Click(object sender, EventArgs e)
{
//打圖像文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "圖像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 圖像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 圖像文件(*.gif)|*.gif |BMP圖像文件(*.bmp)|*.bmp|Tiff圖像文件(*.tif;*.tiff)|*.tif;*.tiff|Png圖像文件(*.png)| *.png |所有文件(*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Bitmap MyBitmap = new Bitmap(openFileDialog.FileName);
this.pictureBox1.Image = MyBitmap;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int Height = this.pictureBox1.Image.Height;//獲取圖像高度
int Width = this.pictureBox1.Image.Width;//獲取圖像寬度
Bitmap bitmap = new Bitmap(Width, Height);//實例化新的位圖對象
Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;//記錄原圖
Color pixel;//定義一個Color結構
int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 };//定義高斯模板值
//遍歷原圖的每個位置
for (int x = 1; x < Width - 1; x++)
for (int y = 1; y < Height - 1; y++)
{
int r = 0, g = 0, b = 0;//聲明3個變量,用來記錄R/G/B值
int Index = 0;//聲明一個變量,用來記錄位置
for (int col = -1; col <= 1; col++)
for (int row = -1; row <= 1; row++)
{
pixel = MyBitmap.GetPixel(x + row, y + col);//獲取指定點的像素
r += pixel.R * Gauss[Index];//記錄R值
g += pixel.G * Gauss[Index];//記錄G值
b += pixel.B * Gauss[Index];//記錄B值
Index++;
}
r /= 16;//爲R重新賦值
g /= 16;//爲G重新賦值
b /= 16;//爲B重新賦值
//處理顏色值溢出
r = r > 255 ? 255 : r;
r = r < 0 ? 0 : r;
g = g > 255 ? 255 : g;
g = g < 0 ? 0 : g;
b = b > 255 ? 255 : b;
b = b < 0 ? 0 : b;
bitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));//重新爲指定點賦顏色值
}
this.pictureBox1.Image = bitmap;//顯示柔化效果的圖像
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}