目錄
實作垂直百葉窗效果。
水準交錯顯示圖像
紋理效果展示
實作浮雕效果
實作膠片效果
實作積木效果
柔化效果顯示圖檔
原理:将圖像分成若幹個區域,各個區月以一種漸進的方式逐漸顯示,效果就像百葉窗翻動一樣。主要用到了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; //顯示打開的圖檔
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); //線程挂起
實作的原理:使用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類
this.BackgroundImage = myBitmap;//顯示打開的圖檔
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;//顯示設定後的圖檔
實作原理:通過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)
myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName); //根據檔案的路徑執行個體化Image類
this.BackgroundImage = myBitmap; //顯示打開的圖檔
//周遊圖檔中的所有象素
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; //顯示處理後的圖檔
}
實作原理:對現有的圖像取反色。
{
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。
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp";
openFileDialog1.ShowDialog();
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);
this.BackgroundImage = myImage;
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;
int Height = this.pictureBox1.Image.Height;//擷取圖像高度
int Width = this.pictureBox1.Image.Width;//擷取圖像寬度
Bitmap bitmap = new Bitmap(Width, Height);//執行個體化新的位圖對象
Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;//記錄原圖
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;//顯示柔化效果的圖像
MessageBox.Show(ex.Message, "資訊提示");