2017-08-31 11 views
-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'を取得します。

私は間違っていますか?


機能:あなたが意味Streamことで

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; 
} 

答えて

4

は、あなたがそれを使用する前に、ストリームを初期化する必要があります。

using (MemoryStream myStream = new MemoryStream()) { 
    screenshot.Save(myStream, System.Drawing.Imaging.ImageFormat.Gif); 
} 
+1

ストリームを閉じて処理するためのポイント。 –

+0

@BradleyUffner - 一貫性があるのが良いですが、 'MemoryStream'インスタンスを処分する必要はありません。解放する必要がある管理されていないリソースはありません。 'MemoryStream'の' Dispose'実装を見ると、実際には何も起こりません。 – Igor

4

FileStreamMemoryStream、何?どのような場合でも、変数を必要なストリームの適切な種類に初期化するだけです。例えば

using(var myStream = new MemoryStream()) 
{....} 
関連する問題