2014-11-24 58 views
0

在WinForms(最好是C#)中,我怎樣才能製作一個簡單的「縮放」工具來在光標位置下方顯示一個矩形視圖?理想情況下,只能放大控制(按鈕,標籤...).NET中的簡單縮放工具

雖然首先,標準庫(.dll)是否可以這樣做?我是與圖形工作完全新手...

在此先感謝!

編輯:此問題/答案(Zoom a Rectangle in .NET)處理有關縮放圖像,而不是輸入控件。我只想放大控制。


編輯2:通過每個控件的MouseEnter事件我定位一個面板,該面板應該包含控件的圖像放大。我只得到了面板在正確的網站...

private void anyControl_MouseEnter(object sender, EventArgs e) 
{ 
    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
        Screen.PrimaryScreen.Bounds.Height); 
    //Create the Graphic Variable with screen Dimensions 
    Graphics graphics = Graphics.FromImage(printscreen as Image); 
    //Copy Image from the screen 
    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

    Control auxControl = (Control) sender; 
    panel.Width = auxControl + 20; 
    panel.Height = auxControl + 20; 
    panel.Location = new Point (auxControl.Location.X - 10, auxControl.Location.Y - 10); 

    control.DrawToBitmap(printscreen, panel.Bounds) 
} 

回答

1

那麼你可以從這段代碼的屏幕圖像(感謝http://www.codeproject.com/Articles/485883/Create-your-own-Snipping-Tool):

Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
         Screen.PrimaryScreen.Bounds.Height); 
//Create the Graphic Variable with screen Dimensions 
Graphics graphics = Graphics.FromImage(printscreen as Image); 
//Copy Image from the screen 
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

因此採用這種和鏈接你共享你應該排序

,或者如果你不熱衷於使用DrawToBitmap法的形式對捕獲屏幕部分(感謝Capture screenshot of only a section of a form?

+1

這是一個好的開始。接下來的步驟是:1.創建一個面板並將其放置在每個鼠標移動後跟隨光標。 2.用答案中的代碼顯示您抓取的位圖的放大版本。 (使用帶有兩個矩形的DrawImage!)3.確保在需要再次抓取屏幕或表單時隱藏面板。 – TaW 2014-11-24 19:22:47

0

隨着下面的代碼我解決了我的問題,但最終的圖像是一些模糊的...(我「縮放」到20%的圖像)

private void anyControl_MouseEnter(object sender, EventArgs e) 
{ 

    Control auxControl = (Control) sender; 

    int enlargedWidth = (int) Math.Round(auxControl.Width * 1.20); 
    int enlargedHeight = (int) Math.Round(auxControl.Height * 1.20); 

    panel.Width = enlargedWidth; 
    panel.Height = enlargedHeight; 
    panel.Location = new Point (auxControl.Location.X - (int) Math.Round(auxControl.Width * 0.10), auxControl.Location.Y - (int) Math.Round(auxControl.Height * 0.10)); 

    Bitmap aBitmap = new System.Drawing.Bitmap(auxControl.Width, auxControl.Height); 
    auxControl.DrawToBitmap(aBitmap, auxControl.ClientRectangle); 

    Bitmap aZoomBitmap = ZoomImage(aBitmap, panel.Bounds); 
    panel.ContentImage = aZoomBitmap; 

    panel.Visible = true; 
} 

private Bitmap ZoomImage(Bitmap pBmp, Rectangle pDestineRectangle) 
{ 

    Bitmap aBmpZoom = new Bitmap(pDestineRectangle.Width, pDestineRectangle.Height); 
    Graphics g = Graphics.FromImage(aBmpZoom); 
    Rectangle srcRect = new Rectangle(0, 0, pBmp.Width, pBmp.Height); 
    Rectangle dstRect = new Rectangle(0, 0, aBmpZoom.Width, aBmpZoom.Height); 
    g.DrawImage(pBmp, dstRect, srcRect, GraphicsUnit.Pixel); 

    return aBmpZoom; 
}