-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