2012-08-16 134 views
1

我正在使用C#制作放大镜工具。很像这样:http://colorsnapper.com 我在Google上搜索了一种放大屏幕预定义区域的方法,足以查看每个像素。放大屏幕的特定区域

更具体地说,我希望我的鼠标成为屏幕上的放大镜,增强鼠标悬停在每个像素上。我需要弄清楚如何放大预定义的区域。

有没有人知道我可以做到这一点的方式,或任何可用的API。

UPDATE 我发现,微软已经提供了放大API:http://msdn.microsoft.com/en-us/library/windows/desktop/ms692402(v=vs.85).aspx然而,这个API是C++。正如我所收集的,C++是Windows操作系统编写的内容,为了使用此API,我需要使用某种C#包装器。这不是一个问题,我只是认为我会为其他用户添加到该帖子中。

+1

如果您最初的问题未能生成响应,您应该编辑它而不是发布一个新的。 – 2012-08-16 20:20:43

回答

4

你可以捕获屏幕的位图存储器中:

/// <summary> 
/// Saves a picture of the screen to a bitmap image. 
/// </summary> 
/// <returns>The saved bitmap.</returns> 
private Bitmap CaptureScreenShot() 
{ 
    // get the bounding area of the screen containing (0,0) 
    // remember in a multidisplay environment you don't know which display holds this point 
    Rectangle bounds = Screen.GetBounds(Point.Empty); 

    // create the bitmap to copy the screen shot to 
    Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height); 

    // now copy the screen image to the graphics device from the bitmap 
    using (Graphics gr = Graphics.FromImage(bitmap)) 
    { 
      gr.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size); 
    } 

    return bitmap; 
} 

再取图像的一部分可能通过50像素的矩形一个50像素在鼠标位置为中心:

portionOf = bitmap.Clone(new Rectangle(pointer.X - 25, pointer.Y - 25, 50, 50), PixelFormat.Format32bppRgb); 

而以中心在鼠标位置的100px×100px的矩形显示它。这会给你一个2倍的缩放级别。 (显示尺寸)/(拍摄尺寸)的比率越大,放大得越多。一些沿线的:

[DllImport("User32.dll")] 
public static extern IntPtr GetDC(IntPtr hwnd); 

[DllImport("User32.dll")] 
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc); 

void OnPaint() 
{ 
    IntPtr desktopDC = GetDC(IntPtr.Zero); // Get the full screen DC 

    Graphics g = Graphics.FromHdc(desktopDC); // Get the full screen GFX device 

    g.DrawImage(portionOf, pointer.X - 50, pointer.Y - 50, 100, 100); // Render the image 

    // Clean up 
    g.Dispose(); 
    ReleaseDC(IntPtr.Zero, desktopDC); 
} 
+2

给男人一条鱼......等等,不,OP只是继承了加工厂。 – Shibumi 2012-08-16 21:20:13