2012-10-21 10 views
26

任意のヒント私はasp.netのWeb APIからの映像出力に次のコードをしようとしているが、レスポンスボディの長さは常に0出力画像HttpResponseMessage

public HttpResponseMessage GetImage() 
{ 
    HttpResponseMessage response = new HttpResponseMessage(); 
    response.Content = new StreamContent(new FileStream(@"path to image")); 
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); 

    return response; 
} 

のですか?

WORKS:

[HttpGet] 
    public HttpResponseMessage Resize(string source, int width, int height) 
    { 
     HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); 

     // Photo.Resize is a static method to resize the image 
     Image image = Photo.Resize(Image.FromFile(@"d:\path\" + source), width, height); 

     MemoryStream memoryStream = new MemoryStream(); 

     image.Save(memoryStream, ImageFormat.Jpeg); 

     httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray()); 

     httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); 
     httpResponseMessage.StatusCode = HttpStatusCode.OK; 

     return httpResponseMessage; 
    } 
+0

私はあなたがいるかどうかは確信していませんが、あなたは[** outputStreamに画像を書き込む方法**]をチェックできます(http://stackoverflow.com/questions/5629251/c-sharp-outputting-image-to -response-output-stream-giving-gdi-error) –

+0

エラーは表示されません。何も受け取っていません。 – lolol

+0

あなたはどのステータスコードを取得していますか? –

答えて

5

  1. パスが正しい(当たり前)であることを確認し

  2. あなたのルーティングが正しいことを確認してください。あなたのコントローラがImageControllerであるか、他のコントローラで "GetImage"をサポートするカスタムルートを定義しています。 (あなたがこのために404応答を取得する必要があります。)

  3. あなたはストリームを開くことを確認します

    var stream = new FileStream(path, FileMode.Open);

私は似たような試み、それが私のために動作します。

+0

ありがとう、私の更新のコードが動作します。 – lolol

1

ByteArrayContentの代わりに、StreamContentクラスを使用してストリームで効率を上げることもできます。

関連する問題