-2
我想创建一个截图,并将其从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;
}
“使用”关闭和处置流的点数。 –
@BradleyUffner - 尽管一致性很好,但您不必处置一个“MemoryStream”实例,也不需要释放非托管资源。如果你看看'MemoryStream'中的Dispose实现,实际上并没有发生。 – Igor