如何使用异步方法将C#中的映像发送到客户端

如何使用异步方法将C#中的映像发送到客户端,c#,asynchronous,highcharts,bitmap,C#,Asynchronous,Highcharts,Bitmap,我有两份申请书。一个是服务,另一个是客户端。我的服务使用HighCharts.NET创建图表。然后我将图表发送到外部服务器,并以图像的形式获取图表。这个方法需要是异步的,因为我必须等待,直到我从服务器变成映像。嗯,那很好。 代码如下: public async Task CreateChart(HttpMessage message) { var settings = new HighchartsSetting { Expo

我有两份申请书。一个是服务,另一个是客户端。我的服务使用HighCharts.NET创建图表。然后我将图表发送到外部服务器,并以图像的形式获取图表。这个方法需要是异步的,因为我必须等待,直到我从服务器变成映像。嗯,那很好。 代码如下:

    public async Task CreateChart(HttpMessage message)
    {
        var settings = new HighchartsSetting
        {
            ExportImageType = "png",
            ImageWidth = 1500,
            ServerAddress = "http://export.highcharts.com/"
        };

        var client = new HighchartsClient(settings);

        if (message.Message == "Request was successfully")
        {
            // Get Chart Data
            var results = getResults();

            var chartOptions = new
            {
                title = new
                {
                  text = "TestChart"  
                },
                xAxis = new
                {
                    categories =  getDates();
                },
                series = new[]
                {
                    new { data = getData() }
                }
            };

            var link = await client.GetChartImageFromOptionsAsync(JsonConvert.SerializeObject(chartOptions));

            string preLink = System.Text.Encoding.UTF8.GetString(link);

            string highChartsLink = "http://export.highcharts.com/" + preLink;

            byte[] file;
            using (var downloadClient = new WebClient())
            {
                file = downloadClient.DownloadData(highChartsLink);
            }
            _chartFile = file;
        }
如您所见,现在我的图像在_chartFile变量中是byte[]格式的

现在我有了控制器:

  public async Task<Image> GetRequest([FromBody]ChartRequestModel body)
    {
        ....
        // Creates Chart based on request
        await highChart.CreateChart(message);

        byte[] file = highChart.getChartFile();
        using (Image image = Image.FromStream(new MemoryStream(file)))
        {
            return image;
        }
    }
现在我的问题是,客户收到: response.content=System.Drawing.Bitmap 但我希望它接收位图图像,而不仅仅是它的类型。 我为我的客户使用RestSharp

那么,如何接收图像而不是数据类型呢


感谢帮助

您是否尝试过base64编码字节并将其作为参数发送到响应中?

您是否尝试过base64编码字节并将其作为参数发送到响应中?

我将在
控制器上使用该方法
并返回
操作结果

public async Task<ActionResult> GetRequest([FromBody]ChartRequestModel body)
{
    ....
    // Creates Chart based on request
    await highChart.CreateChart(message);

    byte[] file = highChart.getChartFile();
    return File(file, "image/png"); //or image/jpg, etc.
}
公共异步任务GetRequest([FromBody]ChartRequestModel body)
{
....
//根据请求创建图表
等待highChart.CreateChart(消息);
byte[]file=highChart.getChartFile();
返回文件(文件,“image/png”);//或image/jpg等。
}
现在,您返回的对象不是
ActionResult
,ASP.NET MVC默认情况下会返回
ToString()
,而不是
ActionResult
,我会在
控制器上使用该方法,并返回
ActionResult

public async Task<ActionResult> GetRequest([FromBody]ChartRequestModel body)
{
    ....
    // Creates Chart based on request
    await highChart.CreateChart(message);

    byte[] file = highChart.getChartFile();
    return File(file, "image/png"); //or image/jpg, etc.
}
公共异步任务GetRequest([FromBody]ChartRequestModel body)
{
....
//根据请求创建图表
等待highChart.CreateChart(消息);
byte[]file=highChart.getChartFile();
返回文件(文件,“image/png”);//或image/jpg等。
}

现在您返回的对象不是
ActionResult
,ASP.NET MVC默认返回
ToString()
,而不是
ActionResult

您想返回
位图图像
,所以将返回类型更改为
bitmap
服务是用什么框架编写的?WCF?Asp.net MVC@Michaelyou想要返回
位图图像
,所以将返回类型更改为
位图
服务是用什么框架编写的?WCF?Asp.net MVC@MichaelThank,你可以使用它。我不知道默认情况下它会返回一个字符串。很高兴知道:)谢谢你,它起作用了。我不知道默认情况下它会返回一个字符串。很高兴知道:)
public async Task<ActionResult> GetRequest([FromBody]ChartRequestModel body)
{
    ....
    // Creates Chart based on request
    await highChart.CreateChart(message);

    byte[] file = highChart.getChartFile();
    return File(file, "image/png"); //or image/jpg, etc.
}