C#開發執行個體-訂製螢幕剪取工具(七)添加放大鏡功能的程式碼範例

來源:互聯網
上載者:User

由於時可能需要精確截取某一部分,所以需要放大鏡的功能,這樣截取的時候才更容易定位的位置。

添加PictureBox,name屬性設定為“pictureBox_zoom”;


在“Form1_Load”事件處理函數中添加以下代碼:

//設定放大鏡的大小            this.pictureBox_zoom.Width = this.ZoomBoxWidth;            this.pictureBox_zoom.Height = this.ZoomBoxHeight;

在“ExitCutImage”方法中添加代碼:

在“Form1_MouseUp”事件處理函數中添加代碼:


在“ShowForm”方法的else條件最後添加代碼:

if (this.ZoomBoxVisible)                {                    UpdateCutInfoLabel(UpdateUIMode.ShowZoomBox);                    this.pictureBox_zoom.Show();                }

在“UpdateCutInfoLabel”函數最後添加以下代碼:

if (this.pictureBox_zoom.Visible || (updateUIMode & UpdateUIMode.ShowZoomBox) != UpdateUIMode.None)            {                Point zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y + 22);                if (zoomLocation.Y + this.pictureBox_zoom.Height > this.Height)                {                    if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)                    {                        zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 10, MousePosition.Y - this.pictureBox_zoom.Height - 10);                    }                    else                    {                        zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y - this.pictureBox_zoom.Height - 15);                    }                }                else                {                    if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)                    {                        zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 15, MousePosition.Y);                    }                }                this.pictureBox_zoom.Location = zoomLocation;                if (!this.pictureBox_zoom.Visible)                {                    this.pictureBox_zoom.Show();                }            }

在“Form1_KeyUp”事件處理函數中添加以下代碼:


為“pictureBox_zoom”添加“Paint”事件處理常式,代碼如下:

        /// <summary>        /// 放大鏡組件重繪事件處理常式        /// 即時顯示滑鼠指標位置放大後的映像        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void pictureBox_zoom_Paint(object sender, PaintEventArgs e)        {            Bitmap bmp_lbl = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);            int srcWidth = (int)(this.ZoomBoxWidth / 10);            int srcHeight = (int)(this.ZoomBoxHeight / 10);            Bitmap bmp = new Bitmap(srcWidth, srcHeight);            Rectangle srcRect = new Rectangle(MousePosition.X - 5, MousePosition.Y - 4, srcWidth, srcHeight);            if (!isCuting)            {                srcRect = new Rectangle(MousePosition.X - 6, MousePosition.Y - 5, srcWidth, srcHeight);            }            Graphics g = Graphics.FromImage(bmp);            g.DrawImage(screenImage, 0, 0, srcRect, GraphicsUnit.Pixel);            g.Dispose();            //Zoom            int x, y;            for (int row = 0; row < bmp.Height; row++)            {                for (int col = 0; col < bmp.Width; col++)                {                    Color pc = bmp.GetPixel(col, row);                    for (int h = 0; h < 10; h++)                    {                        for (int w = 0; w < 10; w++)                        {                            x = col * 10 + w;                            y = row * 10 + h;                            if (x < bmp_lbl.Width && y < bmp_lbl.Height)                            {                                bmp_lbl.SetPixel(x, y, pc);                            }                        }                    }                }            }            e.Graphics.DrawImage(bmp_lbl, 0, 0);            int blockX = e.ClipRectangle.Width / 2;            int blockY = e.ClipRectangle.Height / 2;            SolidBrush brush = new SolidBrush(Color.FromArgb(10, 124, 202));            Pen pen = new Pen(brush, 2.0F);            e.Graphics.DrawLine(pen, new Point(0, blockY), new Point(e.ClipRectangle.Width, blockY));            e.Graphics.DrawLine(pen, new Point(blockX, 0), new Point(blockX, e.ClipRectangle.Height));            g.Dispose();            bmp_lbl.Dispose();        }

編譯,運行,看看效果吧!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.