-
using System;
-
using System.Drawing;
-
using System.Drawing.Imaging;
-
using System.Windows.Forms;
-
namespace WindowsApplication_checkBox
-
{
-
public partial class Form1 : Form
-
{
-
public Form1( )
-
{
-
InitializeComponent( );
-
}
-
private void button5_Click( object sender, EventArgs e)
-
{
-
OpenFileDialog ofd =
new OpenFileDialog
( );
-
ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
-
if (ofd.ShowDialog ( ) == DialogResult.OK )
-
{
-
pictureBox1.Image = Image.FromFile (ofd.FileName );
-
}
-
}
-
private void button1_Click( object sender, EventArgs e)
-
{
-
Bitmap b = pictureBox1.Image as Bitmap;
-
if (b == null )
-
{
-
MessageBox.Show ( "請載入圖片" );
-
return;
-
}
-
int width = b.Width;
-
int height = b.Height;
-
BitmapData bd = b.
LockBits ( new Rectangle
( 0,
0, width, height
), ImageLockMode.
ReadOnly, PixelFormat.
Format24bppRgb );
-
IntPtr imgPtr = bd.Scan0;
-
int stride = bd.Stride;
-
int widthByte = width*3;
-
int skipByte = stride - widthByte;
-
int [,,
] rgbData =
new int [width,height,
3 ];
-
#region 讀取 RGB 資料
-
unsafe
-
{
-
byte* p = ( byte*) ( void*) imgPtr;
-
for ( int j = 0; j < height; j++)
-
{
-
for ( int i = 0; i < width; i++)
-
{
-
rgbData[i, j, 2 ] = p[ 0 ];
-
p++;
-
rgbData[i, j, 0 ] = p[ 0 ];
-
p++;
-
rgbData[i, j, 1 ] = p[ 0 ];
-
p++;
-
}
-
p += skipByte;
-
}
-
}
-
#endregion
-
#region 灰階處理
-
unsafe
-
{
-
imgPtr = bd.Scan0;
-
byte* p = ( byte*) ( void*) imgPtr;
-
for ( int j = 0; j < height; j++)
-
{
-
for ( int i = 0; i < width; i++)
-
{
-
int gary = (rgbData[i, j, 0 ] + rgbData[i, j, 1 ] + rgbData[i, j, 2 ] )/3;
-
p[ 0 ] = ( byte ) gary;
-
p++;
-
p[ 0 ] = ( byte ) gary;
-
p++;
-
p[ 0 ] = ( byte ) gary;
-
p++;
-
}
-
p += skipByte;
-
}
-
}
-
#endregion
-
b.UnlockBits (bd);
-
Refresh( );
-
}
-
private void button2_Click( object sender, EventArgs e)
-
{
-
Bitmap b = pictureBox1.Image as Bitmap;
-
if (b == null )
-
{
-
MessageBox.Show ( "請載入圖片" );
-
return;
-
}
-
int width = b.Width;
-
int height = b.Height;
-
BitmapData bd = b.
LockBits ( new Rectangle
( 0,
0, width, height
), ImageLockMode.
ReadOnly, PixelFormat.
Format24bppRgb );
-
IntPtr imgPtr = bd.Scan0;
-
int stride = bd.Stride;
-
int widthByte = width*3;
-
int skipByte = stride - widthByte;
-
int [,,
] rgbData =
new int [width,height,
3 ];
-
#region 讀取 RGB 資料
-
unsafe
-
{
-
byte* p = ( byte*) ( void*) imgPtr;
-
for ( int j = 0; j < height; j++)
-
{
-
for ( int i = 0; i < width; i++)
-
{
-
rgbData[i, j, 2 ] = p[ 0 ];
-
p++;
-
rgbData[i, j, 0 ] = p[ 0 ];
-
p++;
-
rgbData[i, j, 1 ] = p[ 0 ];
-
p++;
-
}
-
p += skipByte;
-
}
-
}
-
#endregion
-
#region 紅色濾鏡
-
unsafe
-
{
-
imgPtr = bd.Scan0;
-
byte* p = ( byte*) ( void*) imgPtr;
-
for ( int j = 0; j < height; j++)
-
{
-
for ( int i = 0; i < width; i++)
-
{
-
p[ 0 ] = 0;
-
p++;
-
p[ 0 ] = 0;
-
p++;
-
p++;
-
}
-
p += skipByte;
-
}
-
}
-
#endregion
-
b.UnlockBits (bd);
-
Refresh( );
-
}
-
private void button3_Click( object sender, EventArgs e)
-
{
-
Bitmap b = pictureBox1.Image as Bitmap;
-
if (b == null )
-
{
-
MessageBox.Show ( "請載入圖片" );
-
return;
-
}
-
int width = b.Width;
-
int height = b.Height;
-
BitmapData bd = b.
LockBits ( new Rectangle
( 0,
0, width, height
), ImageLockMode.
ReadOnly, PixelFormat.
Format24bppRgb );
-
IntPtr imgPtr = bd.Scan0;
-
int stride = bd.Stride;
-
int widthByte = width*3;
-
int skipByte = stride - widthByte;
-
int [,,
] rgbData =
new int [width,height,
3 ];
-
#region 讀取 RGB 資料
-
unsafe
-
{
-
byte* p = ( byte*) ( void*) imgPtr;
-
for ( int j = 0; j < height; j++)
-