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

10、图像的特效显示

        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern int BitBlt(IntPtr hDestDC,int x,int y,int nWidth,int nHeight,IntPtr hSrcDC,int xSrc,int ySrc,int dwRop);
        const int SRCCOPY = 0xCC0020;
        
        private void button1_Click(object sender, System.EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Bitmap bmp;
                bmp=new Bitmap(this.openFileDialog1.FileName);
                this.pictureBox1.Image=bmp;
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            int i,j;
            IntPtr hDC1;
            IntPtr hDC2;
            hDC1=GetDC(this.pictureBox1.Handle);
            hDC2=GetDC(this.pictureBox2.Handle);
            for (i=0;i<this.pictureBox1.Width/10;i++)
            {
                for(j=0;j<10;j++)
                {
                    BitBlt(hDC2,j*this.pictureBox1.Width/10,0,i+2,this.pictureBox1.Height,hDC1,j*this.pictureBox1.Width/10,0,SRCCOPY);
                }
                System.Threading.Thread.Sleep(100); 
            }
        }

11、显示动画光标

        [DllImport("user32")] 
        private static extern IntPtr SetCursor(IntPtr hCursor); 
        [DllImport("user32")] 
        private static extern IntPtr LoadCursorFromFile(string lpFileName); 
        const int WM_SETCURSOR = 0x0020; 

        private void Form1_Load(object sender, System.EventArgs e) 
        { 
            IntPtr hCursor; 
            hCursor=LoadCursorFromFile("..\\..\\stopwtch.ani"); 
            SetCursor(hCursor);     
        } 

        protected override void WndProc(ref System.Windows.Forms.Message m) 
        { 
            switch(m.Msg) 
            { 
                case WM_SETCURSOR: 
                    IntPtr hCursor; 
                    hCursor=LoadCursorFromFile("..\\..\\stopwtch.ani"); 
                    SetCursor(hCursor);     
                    break; 
                default: 
                    base.WndProc(ref m); 
                    break; 
            } 
        }

12、缩放时使用插值模式控制图片质量

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Image image = new Bitmap("..\\..\\test.bmp");
            int width = image.Width;
            int height = image.Height;

            // Draw the image with no shrinking or stretching.
            e.Graphics.DrawImage(
                image,
                new Rectangle(10, 10, width, height),  // destination rectangle 
                0,
                0,           // upper-left corner of source rectangle
                width,       // width of source rectangle
                height,      // height of source rectangle
                GraphicsUnit.Pixel,
                null);

            // Shrink the image using low-quality interpolation.
            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            e.Graphics.DrawImage(
                image,
                new Rectangle(10, 250, (int)(0.6 * width), (int)(0.6*height)), 
                // destination rectangle
                0,
                0,           // upper-left corner of source rectangle
                width,       // width of source rectangle
                height,      // height of source rectangle
                GraphicsUnit.Pixel);

            // Shrink the image using medium-quality interpolation.
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.DrawImage(
                image,
                new Rectangle(150, 250, (int)(0.6 * width), (int)(0.6 * height)),
                // destination rectangle
                0,
                0,           // upper-left corner of source rectangle
                width,       // width of source rectangle
                height,      // height of source rectangle
                GraphicsUnit.Pixel);

            // Shrink the image using high-quality interpolation.
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImage(
                image,
                new Rectangle(290, 250, (int)(0.6 * width), (int)(0.6 * height)),
                // destination rectangle
                0,
                0,           // upper-left corner of source rectangle
                width,       // width of source rectangle
                height,      // height of source rectangle
                GraphicsUnit.Pixel);
        }

13、轻松实现大图像浏览

            this.pictureBox1.SizeMode=PictureBoxSizeMode.AutoSize;
            if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Bitmap b=new Bitmap(this.openFileDialog1.FileName);
                this.pictureBox1.Image=b;
            }


posted @ 2010-12-13 15:57  BuildNewApp  阅读(303)  评论(0编辑  收藏  举报