我正在尝试创建一个屏幕截图并将其从C#发送到我存储它的PHP。
我创建了这样的截图:
Bitmap screenshot = TakeScreenshot();
现在我尝试save it as a stream:
Stream myStream;
screenshot.Save(myStream, System.Drawing.Imaging.ImageFormat.Gif);
但是,我得到Use of unassigned local variable 'myStream'
。
我做错了什么?
功能
private Bitmap TakeScreenshot()
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
using (var gfxScreenshot = Graphics.FromImage(bmpScreenshot))
{
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
return bmpScreenshot;
}
答案 0 :(得分:4)
Stream
你的意思是FileStream
,MemoryStream
,是什么?无论如何,只需将变量初始化为您需要的适当类型的流。
例如:
using(var myStream = new MemoryStream())
{....}
答案 1 :(得分:4)
您必须在使用之前初始化您的流。
using (MemoryStream myStream = new MemoryStream()) {
screenshot.Save(myStream, System.Drawing.Imaging.ImageFormat.Gif);
}