如何在我的两个表格中显示所选区域? [英] How do I display on one of my two forms the selected area?

查看:74
本文介绍了如何在我的两个表格中显示所选区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,通过选择具有BorderStyle None,BackColor Aqua,Opacity 60%,WindowsState Normal和TopMost False属性的Form2的位置和大小,选择屏幕的一部分并将其保存为jpg或PDF文件。它使用Form1中的MouseDown,MouseMove和MouseUp从屏幕上的任何位置选择区域,因为Form1 WindowsState属性设置为Maximized,BorderStyle设置为None,Opacity设置为2%,Cursor to Cross和TopMost设置为True。



该程序可以很好地将所选区域保存为jpg文件或PDF文件。我无法做的是在选择之后但在保存为jpg或PDF文件之前在屏幕上显示所选区域。我尝试了很多东西,但没有任何作用。



以下是感兴趣的区域的代码:



I have a small program that selects a portion of the screen and saves it to a jpg or PDF file by selecting the Location and size of Form2 with properties of BorderStyle None, BackColor Aqua, Opacity 60%, WindowsState Normal and TopMost False. It uses MouseDown, MouseMove and MouseUp in Form1 to select an area from anywhere on the screen, because Form1 WindowsState property is set to Maximized, BorderStyle to None, Opacity to 2%, Cursor to Cross and TopMost to True.

The program does a nice job of saving the selected area to either a jpg file or a PDF file. What I am unable to do is display the selected area on the screen after selection but before saving to either a jpg or PDF file. I have tried a number of things but nothing works.

Here is code from the area of interest:

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        Form2.Show()
        Form2.Location = Cursor.Position 'This is the starting X Y position
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Form2.Size = Cursor.Position - Form2.Location 'This is the size, width & height, (X2-X1 & Y2-Y1)
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        Form2.Hide()
        Me.Hide()
        Dim screenshot = New System.Drawing.Bitmap(Form2.Width, Form2.Height)
        Dim graph As Graphics = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(Form2.Bounds.X, Form2.Bounds.Y, 0, 0, Form2.Size)
        Form2.BackgroundImage = screenshot

        'Need code here to display the selected area before saving as jpg or PDF
        
        Threading.Thread.Sleep(2000)

        If MessageBox.Show("Save as JPG?", "File Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _
                Windows.Forms.DialogResult.Yes Then
            Dim sPath As New SaveFileDialog
            sPath.Filter = "Image(*.jpg)|*,*"
            sPath.ShowDialog()
            Dim bmp As Bitmap = Form2.BackgroundImage
            bmp.Save(sPath.FileName + ".jpg")
        Else
            MessageBox.Show("You are saving as a PDF file")





我尝试过:





What I have tried:


graph.DrawImage(Form2.BackgroundImage, 20, 30)


推荐答案

几件事:

首先,图形上下文是稀缺资源:当你创建一个时,你负责完成后调用Dispose。如果不这样做,整个系统将很快停止工作,因为它依赖于它们完成与在屏幕上绘图相关的所有事情。最简单的方法是在使用块 [ ^ ]它将处理超出范围的事件。

其次,一旦你将所需的部分绘制成位图,你可以用它做任何你喜欢的事情。将PictureBox添加到Form1,并在那里显示:

Couple of things:
First off, Graphics contexts are scarce resources: when you create one, you are responsible for calling Dispose when you are finished with them. If you don't, the whole system will stop working pretty quickly, as it relies on them to do everything related to drawing on the screen. The simplest way is to create it within a Using block[^] which will Dispose of the contect whjen it goes out of scope.
Second, once you have drawn the section you need into a bitmap, you can do anything you like with it. Add a PictureBox to Form1, and display it there:
graph.CopyFromScreen(Form2.Bounds.X, Form2.Bounds.Y, 0, 0, Form2.Size)
MyPictureBox.Image = screenshot


这篇关于如何在我的两个表格中显示所选区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆