C#控制台-通过窗体显示图像

本文关键字:显示 显示图 图像 窗体 控制台 -通 | 更新日期: 2023-09-27 18:27:02

我想从控制台应用程序中显示一个图像,我已经编写了代码。但当它显示图像时,它会显示一个奇怪的Form框。如果我保存图像并正常打开,它会显示图像。

代码:

Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmpScreenCapture);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
    Screen.PrimaryScreen.Bounds.Y,
    0, 0,
    bmpScreenCapture.Size,
    CopyPixelOperation.SourceCopy);
bmpScreenCapture.Save("potato.png", ImageFormat.Png);
Form imageForm = new Form();
//imageForm.FormBorderStyle = FormBorderStyle.None;
imageForm.Controls.Add(new PictureBox() { Image = bmpScreenCapture, Visible = true });
imageForm.Show();

C#控制台-通过窗体显示图像

这是因为您看到的是表单边框。你在正确的轨道上:

imageForm.FormBorderStyle = FormBorderStyle.None;

但是,您需要适当调整窗口的大小。但你根本不需要窗户。调用后

bmpScreenCapture.Save("potato.png", ImageFormat.Png);

只需执行System.Diagnostics.Process.Start("Potato.png");并且它将在默认程序中打开图像。

        Form imageForm = new Form();
        imageForm.Text = "Screenshot";
        //imageForm.FormBorderStyle = FormBorderStyle.None;
        imageForm.Size = bmpScreenCapture.Size;
        imageForm.BackgroundImageLayout = ImageLayout.Zoom;
        imageForm.BackgroundImage = bmpScreenCapture;
        imageForm.ShowDialog();