Clipboard类由Object类派生而来,在应用程序中可以直接调用ClipBoard类,或者构建剪贴板实例来操作。
获取剪贴板的内容:IDataObject iData=System.WinForms.Clipboard.GetDataObject();
复制:setDataObject(str); setDataObject(str,true);//表示应用程序退出后数据依然保留在剪贴板
粘贴:IDataObject iData=System.WinForms.Clipboard.GetDataObject();
if(iData.GetDataPresent(DataFormats.Text)){…}
图像的复制:
/// <summary>
/// 图像的复制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void 复制图像ToolStripMenuItemClick(object sender, EventArgs e)
{
Clipboard.SetDataObject(pictureBox1.Image,true);
}
图像的剪切:
void ClipMenuClick(object sender, EventArgs e)
{
Clipboard.SetDataObject(pictureBox1.Image,true);
pictureBox1.Image=null;
}
图像的粘贴:
void PasteMenuClick(object sender, EventArgs e)
{
IDataObject iData=Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Bitmap) ){
pictureBox1.Image=(Bitmap)iData.GetData(DataFormats.Bitmap);
}
}
图像平滑处理:
void SmoothMenuClick(object sender, EventArgs e)
34: {
35: Bitmap bmp=new Bitmap(pictureBox1.Image);
Bitmap newbmp=new Bitmap(pictureBox1.Image);
37: Color c=new Color();
38: Color NewC;
39: Byte r,g,b;
40: int r2,g2,b2;
41:
42: for (int i=1;i<bmp.Width-1 ;i++ )
43: {
44: for (int j=1; j<bmp.Height-1; j++) {
45: r2=0;g2=0;b2=0;
46: for (int k1=-1; k1<=1; k1++) {
47: for (int k2=-1; k2<=1; k2++) {
48: c=bmp.GetPixel(i+k1,j+k2);
49: r=c.R;
50: g=c.G;
51: b=c.B;
52:
53: r2=r2+r;
54: g2=g2+g;
55: b2=b2+b;
56: }
57: }
58:
59: r2=(Byte)(r2/9);
60: g2=(Byte)(g2/9);
61: b2=(Byte)(b2/9);
62: NewC=Color.FromArgb(r2,g2,b2);
63: newbmp.SetPixel(i,j,NewC);
64: }
65: }
66:
67: // 刷新显示
68: pictureBox2.Refresh();
69: pictureBox2.Image=newbmp;
70: }
梯度图像
73: /// 首先计算源图像的像素(i, j)点的分量值与同行的下一个像素
74: /// (i+1, j)以及相同列的下一个像素(i, j+1)的分量的梯度,
75: /// 即差的平方和的平方根,然后将梯度值作为新的像素重新放回(i, j)
76: /// </summary>
77: void NihongMenuClick(object sender, EventArgs e)
78: {
79: Bitmap bmp=new Bitmap(pictureBox1.Image);
80: Bitmap newbmp=new Bitmap(pictureBox1.Image);
81: Color c1=new Color();
82: Color NewC;
83: int r1,r2,r3,g1,g2,g3,b1,b2,b3,rr,gg,bb;
84:
85: for (int i=0;i<bmp.Width;i++ )
86: {
87: for (int j=0; j<bmp.Height; j++) {
88: c1 = bmp.GetPixel(i, j);
89: r1 = c1.R;
90: g1 = c1.G;
91: b1 = c1.B;
92: if (i+1==bmp.Width) {
93: c1 = bmp.GetPixel(i , j);
94: }
95: else
96: {
97: c1 = bmp.GetPixel(i + 1, j);
98: }
99: r2 = c1.R;
100: g2 = c1.G;
101: b2 = c1.B;
102: if (j+1==bmp.Height) {
103: c1 = bmp.GetPixel(i , j);
104: }
105: else
106: {
107: c1 = bmp.GetPixel(i, j+1);
108: }
109: r3 = c1.R;
110: g3 = c1.G;
111: b3 = c1.B;
112: rr = (int)(255-2 * Math.Sqrt((r1 - r2) * (r1 - r2) + (r1 - r3) * (r1 - r3)));
113: gg = (int)(255-2 * Math.Sqrt((g1 - g2) * (g1 - g2) + (g1 - g3) * (g1 - g3)));
114: bb = (int)(255-2 * Math.Sqrt((b1 - b2) * (b1 - b2) + (b1 - b3) * (b1 - b3)));
115: NewC=Color.FromArgb((Byte)rr,(Byte)gg,(Byte)bb);
116: newbmp.SetPixel(i,j,NewC);
117: }
118: }
119:
120: // 刷新显示
121: pictureBox2.Refresh();
122: pictureBox2.Image=newbmp;
123: }
如何让窗体画图时,可以根据图片大小自动缩放:
if (curBitmap != null)
{
Graphics g = e.Graphics;
// g.InterpolationMode = InterpolationMode.Bicubic;
Brush fillbrush=Brushes.White;
//判断图片的大小,如果比窗体大,则进行缩放,否则按照原始大小显示
if (curBitmap.Width > formWidth || curBitmap.Height > formHeight)
{
//图片大小大于窗体 设置窗体大小
this.Width = curBitmap.Width;
this.Height = curBitmap.Height;
Rectangle formRect = new Rectangle(0,0,this.Width,this.Height);
g.FillRectangle(fillbrush, formRect);
g.DrawImage(curBitmap, formRect);
}
else
{
//图片大小小于窗体
this.Width = 800;
this.Height = 600;
Rectangle formRect = new Rectangle(0, 0, formWidth, formHeight);
g.FillRectangle(fillbrush, formRect);
g.DrawImage(curBitmap, 0, 0, curBitmap.Width, curBitmap.Height);
}
设置窗体自动出现滚动条
if(DialogResult.OK == openFileDialog.ShowDialog())
{
m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
this.AutoScroll = true;
this.AutoScrollMinSize=new Size ((int)(m_Bitmap.Width),(int)
m_Bitmap.Height));
this.Invalidate();
}
程序风格:
最好将对图像进行处理的操作如:反色、灰度化、加亮等写成函数,将它们作为一个图像操作类的静态函数(static),返回值为bool型,
根据返回值决定窗体是否重绘。
public static bool Brightness(Bitmap b, int nBrightness)
{
if (nBrightness < -255 || nBrightness > 255)
return false;
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width,
b.Height), ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nVal = 0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;
for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
nVal = (int) (p[0] + nBrightness);
if (nVal < 0) nVal = 0;
if (nVal > 255) nVal = 255;
p[0] = (byte)nVal;
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}