【问题标题】:GetResponseAsync png link url - Parameter not validGetResponseAsync png 链接 url - 参数无效
【发布时间】:2020-04-11 10:52:47
【问题描述】:

我必须编写一个从 url 打印 png 的 Windows 窗体应用程序,我想异步编写代码。 这是我的工作代码(同步)

public Bitmap GetImage()
{
    Bitmap bmp;
    var request = WebRequest.Create("http://www.basilicasanmarco.it/wp-content/themes/tema-basilicasmarco/library/images/patriarcato.png");
    using (var response = request.GetResponse())
    using (var stream = response.GetResponseStream())
    {
        bmp = new Bitmap(stream);
    }
    return bmp;
}

private void PrintImage(PaintEventArgs e)
{
    using (var imag = GetImage())
    {
        if (imag == null)
            return;
        var newHeight = mainImage.Height;
        var rapporto = newHeight / (double) imag.Height;
        var newWidth = Convert.ToInt32(rapporto * imag.Width);
        var imageLocationX = Convert.ToInt32((mainImage.Width - newWidth) / 2.0);
        e.Graphics.DrawImage(imag, imageLocationX, 0, newWidth, newHeight);
    }
}

现在我想以异步等待的方式编写相同的代码,这是我的 sn-p:

public async Task<Bitmap> GetImage()
{
    Bitmap bmp;
    var request = WebRequest.Create("http://www.basilicasanmarco.it/wp-content/themes/tema-basilicasmarco/library/images/patriarcato.png");
    using (var response = await request.GetResponseAsync())
    using (var stream = response.GetResponseStream())
    {
        bmp = new Bitmap(stream);
    }
    return bmp;
}

private async Task PrintImageAsync(PaintEventArgs e)
{
    using (var imag = await GetImage())
    {
        var newHeight = mainImage.Height;
        var rapporto = newHeight / (double) imag.Height;
        var newWidth = Convert.ToInt32(rapporto * imag.Width);
        var imageLocationX = Convert.ToInt32((mainImage.Width - newWidth) / 2.0);
        e.Graphics.DrawImage(imag, imageLocationX, 0, newWidth, newHeight);
    }
}

第一个同步代码运行良好,当我转到异步部分时,我在

中出现“参数无效”异常(System.Drawing 异常)
e.Graphics.DrawImage(imag, imageLocationX, 0, newWidth, newHeight);

代码。 得到的 Bitmap 看起来没问题,它有正确的 Width 和 Height 但里面有问题,只是在异步方法中。

我做错了什么?

更新: 我通过以下方式等待Panel的Paint方法中的方法:

this.panel.Paint += new System.Windows.Forms.PaintEventHandler(this.mainImage_Paint);

private async void mainImage_Paint(object sender, PaintEventArgs e)
{
    await PrintImageAsync(e);
}

我想要获得的是在 Panel 而不是 PictureBox 中绘制下载的图像,因为我需要更底层的方法。

【问题讨论】:

  • 如何调用 PrintImageAsync?你从哪里得到 PaintEventArgs?您是在 Paint 事件中调用它吗?缺少那部分,但关键是要找出问题所在
  • 你怎么称呼这件事,因为图形指针可能没有指向图片框的当前图形或下载图像时你试图绘制的任何东西。
  • @OguzOzgul 很有趣,我没想到。正如您猜想的那样,我添加了这个问题,我在 Paint 事件中调用。我想在面板中打印下载的图像。
  • 正如我所说,Graphics 对象不再有效。当您等待时,Paint 的调用者不会。以这种方式在 Paint 中执行此操作也会导致其他问题。您是否希望每次调用 Paint 事件时都下载图像?我不这么认为。我现在发布一个答案,如果有帮助请告诉我。
  • 你说得对:我只将位图视为错误的根源,但我错了:位图与异步实现无关,问题在于 Paint 方法中的 Graphics 对象。非常感谢您的帮助。

标签: c# asynchronous async-await httpwebrequest


【解决方案1】:

每次重新绘制控件都会调用 Paint 事件处理程序,您可能不希望每次发生这种情况时都下载图像。

当调用事件处理程序时,似乎访问事件 args (e) 参数中的图形对象在异步操作之前成功,但在等待之后失败。

因此;

不清楚是什么触发了源代码中图像的下载,但如果尝试异步执行,请异步获取图像并将其保存在字段中,如果可用,将其绘制在面板上。

下面假设你直接下载图片,没有任何用户操作,这个地方就是async void Form_Load():(可以看到图片是在后台下载的!)

private async void Form1_Load(object sender, EventArgs e)
{
    this.panel1.Paint += this.mainImage_Paint;
    downloadedBitmap = await GetImage();
    // When we got the image, invalidate the panel to trigger a re-paint
    this.panel1.Invalidate();
}

// Keep the downloaded image in a field:
private Image downloadedBitmap;

private async void mainImage_Paint(object sender, PaintEventArgs e)
{
    if (downloadedBitmap != null)
    {
        var newHeight = mainImage.Height;
        var rapporto = newHeight / (double)downloadedBitmap.Height;
        var newWidth = Convert.ToInt32(rapporto * downloadedBitmap.Width);
        var imageLocationX = Convert.ToInt32((mainImage.Width - newWidth) / 2.0);
        e.Graphics.DrawImage(downloadedBitmap, imageLocationX, 0, newWidth, newHeight);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 2020-07-18
    • 1970-01-01
    • 2018-12-22
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多