简体   繁体   中英

C# Image saved is just completely black?

When I tried to save an image of the form by clicking the export button I get a fully black image. I tried png, jpeg.

private void exportButton_Click(object sender, RoutedEventArgs e)
{
    ScreenShot.CaptureImage(this.Location, new System.Drawing.Point(Convert.ToInt32(this.Location.X) + Convert.ToInt32(this.Width) , Convert.ToInt32(this.Location.Y)  + Convert.ToInt32(this.Height)), new Rectangle(this.Location.X, this.Location.Y, Convert.ToInt32(this.Width) , Convert.ToInt32(this.Height)), @"C:\Users\Jens\Desktop\test.Bmp");
}

The class I am using:

public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
{
    using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
        }
        bitmap.Save(FilePath, ImageFormat.Bmp);
    }
}

It was not an easy one. So below a working example using WPF and WinForms.

I found out that when the display is scaled from the OS settings, it comes in play when working with the raw graphics (regarding coordinates/sizes). 在此处输入图像描述 In the WPF example I called this SCALE_FACTOR . Because my windows scale is set to 150%, I scale with factor 1.5 . WPF example works then for me:

WPF example:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private const double SCALE_FACTOR = 1.5; // 150% in windows 10 settings

    private void button_Click(object sender, RoutedEventArgs e)
    {
        // take window location
        var x = (int)(Application.Current.MainWindow.Left);
        var y = (int)(Application.Current.MainWindow.Top);
        // convert to System.Drawing.Point
        var currentLocationPoint = new System.Drawing.Point(x, y);
        CaptureImage(
            // current TOP, LEFT location of the window on the screen
            currentLocationPoint, 
            // TOP, LEFT location of the destination
            new System.Drawing.Point(0, 0), 
            // size of the current window AND of the destination window/buffer/image
            new System.Drawing.Rectangle(
                0, 
                0, 
                // need to scale the width because of the OS scaling
                (int)(this.ActualWidth * SCALE_FACTOR),
                // need to scale the heigt because of the OS scaling
                (int)(this.ActualHeight * SCALE_FACTOR)), 
            @"c:\temp\screen.bmp");
    }

    public static void CaptureImage(System.Drawing.Point SourcePoint, System.Drawing.Point DestinationPoint, System.Drawing.Rectangle SelectionRectangle, string FilePath)
    {
        // create new image with the specified size = current window size
        using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(
                    // X start for copy is current window LEFT - scaled
                    (int)(SourcePoint.X * SCALE_FACTOR), 
                    // Y start for copy is current window TOP - scaled
                    (int)(SourcePoint.Y * SCALE_FACTOR), 
                    // X start in destination window is 0
                    0, 
                    // Y start in destination window is 0
                    0, 
                    // size of destination window is the same as the current window
                    SelectionRectangle.Size);
            }
            bitmap.Save(FilePath, ImageFormat.Bmp);
        }
    }
}

Windows Forms example:

In my test the windows has the size of (250,250) in pixels.

SourcePoint is the start, so it should be actually (0,0) . DestinationSize is height and width - you don't need the conversions.

    //CaptureImage(this.Location, new Point(0, 0), new Rectangle(0, 0, 250, 250), @"c:\temp\screen.bmp");
    CaptureImage(this.Location, new System.Drawing.Point(0, 0), new Rectangle(this.Location.X, this.Location.Y, this.Width , this.Height),@"c:\temp\screen.bmp");

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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