2012-01-19 10 views
16

で表示するには、コントローラからの画像を送信します。私は、system.drawingによるテキストを使用してビットマップを作成しています。私は
はViewDataをの私のビューに私のコントローラからその画像を送信したいのですが、私の見解で、私は適切にViewDataをを解析することはできません。は、私はasp.netのmvc3を使用していますMVC3

これは、コントローラのコードです:

public ActionResult About(string agha) 
     { 
      agha = "asgdjhagsdjgajdga"; 
      Color BackColor = Color.White; 
      String FontName = "Times New Roman"; 
      int FontSize = 25; 
      int Height = 50; 
      int Width = 700; 

      Bitmap bitmap = new Bitmap(Width, Height); 
      Graphics graphics = Graphics.FromImage(bitmap); 
      Color color = Color.Gray; ; 
      Font font = new Font(FontName, FontSize);   

      SolidBrush BrushBackColor = new SolidBrush(BackColor); 
      Pen BorderPen = new Pen(color); 

      Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1)); 

      graphics.FillRectangle(BrushBackColor, displayRectangle); 
      graphics.DrawRectangle(BorderPen, displayRectangle);    

      graphics.DrawString(agha,font,Brushes.Red, 0, 0); 
      ViewData["picture"] = bitmap;    

      return View(); 
     } 

のViewDataを呼び出すビューは、この

<img src="@ViewData["picture"]." /> 
+0

コードビューページにこのような – Agha

+1

を知っチェック – Agha

答えて

15

私は完全に役に立たないと退屈なGDI +インフラストラクチャコードを使用してコントローラのアクションを汚染を避けるためにカスタムアクションの結果を書いて、あなたをお勧めします:

public class ImageResult : ActionResult 
{ 
    private readonly string _agha; 
    public ImageResult(string agha) 
    { 
     _agha = agha; 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     Color BackColor = Color.White; 
     String FontName = "Times New Roman"; 
     int FontSize = 25; 
     int Height = 50; 
     int Width = 700; 

     using (Bitmap bitmap = new Bitmap(Width, Height)) 
     using (Graphics graphics = Graphics.FromImage(bitmap)) 
     { 
      Color color = Color.Gray; 
      Font font = new Font(FontName, FontSize); 

      SolidBrush BrushBackColor = new SolidBrush(BackColor); 
      Pen BorderPen = new Pen(color); 

      Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1)); 

      graphics.FillRectangle(BrushBackColor, displayRectangle); 
      graphics.DrawRectangle(BorderPen, displayRectangle); 

      graphics.DrawString(_agha, font, Brushes.Red, 0, 0); 

      context.HttpContext.Response.ContentType = "image/jpg"; 
      bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg); 
     } 
    } 
} 

、その後、単純にこのカスタムアクションの結果を返しますコントローラのアクションを定義します。

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult Image(string agha) 
    { 
     return new ImageResult(agha); 
    } 
} 
(私の例では ~/Views/Home/Index.cshtml)いくつかのビュー内

とから、あなたが動的にあなたのための画像を生成するアクションを参照することができます:

<img src="@Url.Action("Image", new { agha = "foo bar" })" alt="" /> 

あなたが構築するための追加的なアクションを作成したくない場合は別の可能性Data URI schemeを使用することです。基本的に、このイメージをBase64データとして直接HTMLに埋め込むことができます。 1つの注意点は、すべてのブラウザでサポートされているわけではなく、HTMLページがはるかに大きくなることです。

+0

@Darin場合、あなたは私を教えてもらえていないMVCプロジェクトとがある場合はありません 'カミソリビューPage..Pleaseにそれを送信する方法、その場合にはcontext' http://stackoverflow.com/questions/33361412/sending-image-from-rest-service-to-razor-view-page – Lara

2

のように見えますがContentResultを試してみました。

あなたは(検証されていない)は、次のような何かを試すことができ

public ContentResult GetImage() 
     { 
      var content = new ContentResult(); 
      content.Content = "Image as String"; 
      content.ContentType = "image/jpg"; 
      return content; 
     } 
+0

http://zombie-programming.blogspot.com/ – Agha

12

ViewData内にある必要がない場合(各イメージ(イメージ、フラッシュ、ページ自体)が変更されないContentTypeを持つため、ViewDataで実行できるかどうかはわかりません)現在のリクエスト

しかし、あなたはあなたのコントローラでこれを試すことができます。

<img src='@Url.Action("GenerateImage")' alt="" /> 
+1

を見てどうもありがとう:)) – Agha

1
public ContentResult GetImage() 
     { 
      var content = new ContentResult(); 
      content.Content = "Image as String"; 
      content.ContentType = "~image/jpg"; 
      return content; 
     } 
:あなたのビューで

public ActionResult GenerateImage() { 
    FileContentResult result; 

    using(var memStream = new System.IO.MemoryStream()) { 
     bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     result = this.File(memStream.GetBuffer(), "image/jpeg"); 
    } 

    return result; 
} 

を、あなたはコントローラ/アクションを呼び出す必要があります

1
public ActionResult GenerateImage() { 
    FileContentResult result; 

    using(var memStream = new System.IO.MemoryStream()) { 
     bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     result = this.File(memStream.GetBuffer(), "image/jpeg"); 
    } 

    return result; 
} 
関連する問題