在其他 class 中创建图像时获取图像并将其设置为 Picturebox
Get and Set Image to Picturebox when Image was creating in other class
我每天都在尝试学习有关 C# 的新知识。现在我试图从桌面获取图像,我在另一个 class 中完成了。现在我不知道这是怎么回事。如何获取在我的 screenshot.cs 中创建的图像,以便我可以将其设置为我的 Form1.cs
中的图片框
我的代码是这样的:
namespace CatchAreaToImage
{
public partial class Form1 : Form
{
Bitmap screen;
public Form1()
{
InitializeComponent();
}
Bitmap screen2;
private void button1_Click(object sender, EventArgs e)
{
Screenshot.CaptureScreen(screen2);
pBArea.Image = screen2;
}
我的Screenshot.cs是这样的:
class Screenshot
{
public static void CaptureScreen() //do screenshot of desktop
{
// Take an image from the screen
Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Create an empty bitmap with the size of the current screen
Graphics graphics = Graphics.FromImage(screen as Image); // Create a new graphics objects that can capture the screen
graphics.CopyFromScreen(0, 0, 0, 0, screen.Size); // Screenshot moment → screen content to graphics object
}
}
public void Image(Bitmap screen)
{
screen2 = screen;
}
我希望我能正确描述我的问题。
您可以修改您的 CaptureScreen 方法以 return Bitmap 变量屏幕并将其分配给 pictureBox.Image 属性.
class Screenshot
{
public static Image CaptureScreen()
{
Bitmap screen = new Bitmap(
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(screen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, screen.Size);
return screen;
}
}
然后在形式 class 中,您可以进行赋值,因为 Bitmap 继承自 Image
namespace CatchAreaToImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image screen = Screenshot.CaptureScreen();
this.pictureBox1.Image = screen;
// other uses of screen possible
}
}
}
我每天都在尝试学习有关 C# 的新知识。现在我试图从桌面获取图像,我在另一个 class 中完成了。现在我不知道这是怎么回事。如何获取在我的 screenshot.cs 中创建的图像,以便我可以将其设置为我的 Form1.cs
中的图片框我的代码是这样的:
namespace CatchAreaToImage
{
public partial class Form1 : Form
{
Bitmap screen;
public Form1()
{
InitializeComponent();
}
Bitmap screen2;
private void button1_Click(object sender, EventArgs e)
{
Screenshot.CaptureScreen(screen2);
pBArea.Image = screen2;
}
我的Screenshot.cs是这样的:
class Screenshot
{
public static void CaptureScreen() //do screenshot of desktop
{
// Take an image from the screen
Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Create an empty bitmap with the size of the current screen
Graphics graphics = Graphics.FromImage(screen as Image); // Create a new graphics objects that can capture the screen
graphics.CopyFromScreen(0, 0, 0, 0, screen.Size); // Screenshot moment → screen content to graphics object
}
}
public void Image(Bitmap screen)
{
screen2 = screen;
}
我希望我能正确描述我的问题。
您可以修改您的 CaptureScreen 方法以 return Bitmap 变量屏幕并将其分配给 pictureBox.Image 属性.
class Screenshot
{
public static Image CaptureScreen()
{
Bitmap screen = new Bitmap(
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(screen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, screen.Size);
return screen;
}
}
然后在形式 class 中,您可以进行赋值,因为 Bitmap 继承自 Image
namespace CatchAreaToImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image screen = Screenshot.CaptureScreen();
this.pictureBox1.Image = screen;
// other uses of screen possible
}
}
}