简体   繁体   English

如何在 C# 中制作一个简单的放大镜

[英]How to make a simple magnifier in C#

I followed the following in post on "Creating a screen magnifier" .我在“创建屏幕放大镜”的帖子中遵循了以下内容。

Therefore I have this code.因此我有这个代码。 It is not copy & pasted from the post.它不是从帖子中复制和粘贴的。 I have also added a timer so the form is not blank.我还添加了一个计时器,因此表单不是空白的。 However I have found some problems.但是我发现了一些问题。

  1. It doesn't zoom in very much.它不会放大很多。 I would like to have a larger zoom.我想要更大的变焦。 An adjustable zoom setting will be optimal, but I can make that myself if I know how to zoom in more.可调节的缩放设置将是最佳的,但如果我知道如何放大更多,我可以自己制作。
  2. The center of the form is not always the tip of the cursor like I want it would be.表单的中心并不总是像我想要的那样光标的尖端。 Is there anyway I can Fix this?无论如何我可以解决这个问题吗?

Here is the code I have got now.这是我现在得到的代码。

Graphics g;
Bitmap bmp;
private void Timer1_Tick(object sender, EventArgs e)
{
    bmp = new Bitmap(250, 200);
    g = this.CreateGraphics();
    g = Graphics.FromImage(bmp);
    g.CopyFromScreen(MousePosition.X , MousePosition.Y , 0, 0, new Size(300, 300));
    pictureBox1.Image = bmp;
}

The results seem to be exactly the same to this software that I found during my research.结果似乎与我在研究期间发现的这个软件完全相同。 the link , It takes you to a Japanese webpage.链接,它会将您带到一个日语网页。

You're going to have to play around with the various numbers in the example in order to see what effect they have on the output.您将不得不处理示例中的各种数字,以查看它们对输出的影响。 It'll help to turn them into variables so you can play with them more easily.将它们转换为变量会有所帮助,这样您就可以更轻松地使用它们。 Here is a good start, no promises that it works, but it'll give you a good place to start experimenting until you get what you want.这是一个良好的开端,不保证它有效,但它会给你一个开始试验的好地方,直到你得到你想要的东西。

Graphics g;
Bitmap bmp;
private void Timer1_Tick(object sender, EventArgs e)
{
    var endWidth = 300;
    var endHeight = 300;

    var scaleFactor = 2; //perhaps get this value from a const, or an on screen slider

    var startWidth = endWidth / scaleFactor;
    var startHeight = endHeight / scaleFactor;

    bmp = new Bitmap(startWidth, startHeight);

    g = this.CreateGraphics();
    g = Graphics.FromImage(bmp);

    var xPos = Math.Max(0, MousePosition.X - (startWidth/2)); // divide by two in order to center
    var yPos = Math.Max(0, MousePosition.Y - (startHeight/2));

    g.CopyFromScreen(xPos, yPos, 0, 0, new Size(endWidth, endWidth));
    pictureBox1.Image = bmp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM