2012-08-31 4 views
1

왜 그 오류가 발생하는지 실마리가 있습니까?bitmap.Save error

outputStream cannot be null 

고마워요!

public FileContentResult DisplayFont(string fontID) 
     { 
      int fontSize = 12; 

      string fontName = "Arial"; 

      System.Drawing.Font rectangleFont = new System.Drawing.Font(fontName, fontSize, FontStyle.Bold); 

      int height = 150; 

      int width = 250; 

      Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); 

      Graphics g = Graphics.FromImage(bitmap); 

      g.SmoothingMode = SmoothingMode.AntiAlias; 

      Color backgroundColor = Color.White; 

      g.Clear(backgroundColor); 

      g.DrawString(fontName, rectangleFont,SystemBrushes.WindowText, new PointF(10, 40)); 

      Stream outputStream = null; 

      bitmap.Save(outputStream, ImageFormat.Jpeg); // ERROR 

      byte[] byteArray = ReadFully(outputStream); 

      g.Dispose(); 

      bitmap.Dispose(); 

      return new FileContentResult(byteArray, "image/jpeg"); 
     } 



public static byte[] ReadFully(Stream input) 
     { 
      byte[] buffer = new byte[16 * 1024]; 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       int read; 
       while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        ms.Write(buffer, 0, read); 
       } 
       return ms.ToArray(); 
      } 
     } 
+0

@vcsjones을해야합니다. –

답변

3

OutputStream은 null입니다.

int height = 150; 
int width = 250; 
byte []byteArray=null; 

using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb)) 
{ 
    int fontSize = 12; 
    string fontName = "Arial"; 
    System.Drawing.Font rectangleFont = new System.Drawing.Font(fontName, fontSize, FontStyle.Bold); 
    Graphics g = Graphics.FromImage(bitmap); 
    g.SmoothingMode = SmoothingMode.AntiAlias; 
    Color backgroundColor = Color.White; 
    g.Clear(backgroundColor); 
    g.DrawString(fontName, rectangleFont, SystemBrushes.WindowText, new PointF(10, 40)); 

    using (MemoryStream outputStream = new MemoryStream()) 
    { 
     bitmap.Save(outputStream, ImageFormat.Jpeg); 
     byteArray = outputStream.ToArray(); 
    } 
} 
0

내가 해결책을 발견 : 그것은

Stream outputStream = new MemoryStream(); 

편집해야합니다.

이 업데이트

MemoryStream outputStream = new MemoryStream(); 

bitmap.Save(outputStream, ImageFormat.Jpeg); 

byte[] byteArray = outputStream.ToArray();