c# winform 应用编程代码总结 4

14、提取并显示文件包含的图标

        [System.Runtime.InteropServices.DllImport("shell32.dll")]
        private static extern int ExtractIconEx(string lpszFile,int nIconIndex,ref IntPtr phiconLarge,ref IntPtr phiconSmall,int nIcons);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern int DrawIcon(IntPtr hdc, int x,int y,IntPtr hIcon);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                this.Refresh();
                IntPtr Large, Small;
                int i,nIcons;
                Large=(IntPtr)0;
                Small=(IntPtr)0;
                nIcons=ExtractIconEx(this.openFileDialog1.FileName,-1,ref Large,ref Small,1);
                for(i=0;i<nIcons;i++)
                {
                    ExtractIconEx(this.openFileDialog1.FileName,i,ref Large,ref Small,1);
                    DrawIcon(GetDC(this.Handle),(i/4)*40,(i%4)*40,Large);                    
                }
            }
        }

15、抓取并显示程序中的鼠标

        Graphics g;
        private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Cursor.Draw(g,new Rectangle(e.X,e.Y,10,10));
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            g=this.CreateGraphics();
        }

16、图像的局部放大

        Cursor myCursor=new Cursor("..\\..\\MAGNIFY.CUR");
        Graphics g;
        Image myImage;

        private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Cursor.Current=Cursors.Default;
        }

        private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Cursor.Current=myCursor;
            Rectangle sourceRectangle = new Rectangle(e.X-10,e.Y-10,20,20);
            Rectangle destRectangle1 =new Rectangle(300,120,80,80);
            g.DrawImage(myImage,destRectangle1,sourceRectangle,GraphicsUnit.Pixel);
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            g=this.pictureBox1.CreateGraphics();
            myImage=this.pictureBox1.Image;
        }

17、对图像进行浮雕处理

        Bitmap bmp;
        private void button1_Click(object sender, System.EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                bmp=new Bitmap(this.openFileDialog1.FileName);
                for (int i=0;i<bmp.Width-1;i++)
                {
                    for(int j=0;j<bmp.Height-1;j++)
                    {
                        Color Color1=bmp.GetPixel(i,j);
                        Color Color2=bmp.GetPixel(i+1,j+1);
                        int red=Math.Abs(Color1.R-Color2.R+128);
                        int green=Math.Abs(Color1.G-Color2.G+128);
                        int blue=Math.Abs(Color1.B-Color2.B+128);
                        //颜色处理
                        if(red>255)        red=255;
                        if(red<0)        red=0;

                        if(green>255)    green=255;
                        if(green<0)        green=0;

                        if(blue>255)    blue=255;
                        if(blue<0)        blue=0;
                        bmp.SetPixel(i,j,Color.FromArgb(red,green,blue));
                    }
                }
                this.pictureBox1.Image=bmp;
            }
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            this.pictureBox1.SizeMode=PictureBoxSizeMode.StretchImage;
        }

你可能感兴趣的:(WinForm)