提问者:小点点

使用Screenshot.NET库将图像保存到磁盘


希望大家都过得很好。

我正在使用Screenshot.NET库从WPF应用程序中获取屏幕的一个区域绘制的屏幕截图。我遇到的问题是将截图保存到项目文件夹中的目录中。

这是库当前保存映像的方式,但映像没有存储到任何目录:

public static BitmapSource ToBitmapSource(this Bitmap bitmap)
    {
        using (var stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Png);
            var bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(stream.ToArray());
            bitmapImage.EndInit();
            bitmapImage.Freeze();

            return bitmapImage;
        }
    }

我已经尝试指定一个目录保存路径,但是到目前为止还无法让它工作。如能就如何开展工作提出任何建议,将不胜感激。


共1个答案

匿名用户

首先,将位图转换为bytes[]

System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
bytes[] imgBytes = (byte[])converter.ConvertTo(img, typeof(byte[])

然后,使用内置库进行编写。

string basePath="<Your path without File name>";
string fileName="<Your image name with extension>";
System.IO.File.WriteAllBytes(basePath + fileName, imgBytes);