Extract code for reuse, add actual async version

This commit is contained in:
John Lewin 2018-09-29 15:48:41 -07:00
parent be903ef551
commit 87ef7b61cb

View file

@ -54,6 +54,7 @@ namespace MatterHackers.MatterControl
using System.ComponentModel;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading;
@ -1829,44 +1830,14 @@ namespace MatterHackers.MatterControl
/// <param name="uri"></param>
public void DownloadToImageAsync(ImageBuffer imageToLoadInto, string uriToLoad, bool scaleToImageX, IRecieveBlenderByte scalingBlender = null)
{
if (scalingBlender == null)
{
scalingBlender = new BlenderBGRA();
}
WebClient client = new WebClient();
client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) =>
client.DownloadDataCompleted += (sender, e) =>
{
try // if we get a bad result we can get a target invocation exception. In that case just don't show anything
{
// scale the loaded image to the size of the target image
byte[] raw = e.Result;
Stream stream = new MemoryStream(raw);
ImageBuffer unScaledImage = new ImageBuffer(10, 10);
if (scaleToImageX)
{
AggContext.StaticData.LoadImageData(stream, unScaledImage);
// If the source image (the one we downloaded) is more than twice as big as our dest image.
while (unScaledImage.Width > imageToLoadInto.Width * 2)
{
// The image sampler we use is a 2x2 filter so we need to scale by a max of 1/2 if we want to get good results.
// So we scale as many times as we need to get the Image to be the right size.
// If this were going to be a non-uniform scale we could do the x and y separately to get better results.
ImageBuffer halfImage = new ImageBuffer(unScaledImage.Width / 2, unScaledImage.Height / 2, 32, scalingBlender);
halfImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, halfImage.Width / (double)unScaledImage.Width, halfImage.Height / (double)unScaledImage.Height);
unScaledImage = halfImage;
}
Stream stream = new MemoryStream(e.Result);
double finalScale = imageToLoadInto.Width / (double)unScaledImage.Width;
imageToLoadInto.Allocate(imageToLoadInto.Width, (int)(unScaledImage.Height * finalScale), imageToLoadInto.Width * (imageToLoadInto.BitDepth / 8), imageToLoadInto.BitDepth);
imageToLoadInto.NewGraphics2D().Render(unScaledImage, 0, 0, 0, finalScale, finalScale);
}
else
{
AggContext.StaticData.LoadImageData(stream, imageToLoadInto);
}
imageToLoadInto.MarkImageChanged();
this.LoadImageInto(imageToLoadInto, scaleToImageX, scalingBlender, stream);
}
catch
{
@ -1882,6 +1853,61 @@ namespace MatterHackers.MatterControl
}
}
private HttpClient httpClient = new HttpClient();
public async Task LoadRemoteImage(ImageBuffer imageToLoadInto, string uriToLoad, bool scaleToImageX, IRecieveBlenderByte scalingBlender = null)
{
try
{
using (var stream = await httpClient.GetStreamAsync(uriToLoad))
{
this.LoadImageInto(imageToLoadInto, scaleToImageX, scalingBlender, stream);
};
}
catch (Exception ex)
{
Trace.WriteLine("Error loading image: " + uriToLoad);
Trace.WriteLine(ex.Message);
}
}
private void LoadImageInto(ImageBuffer imageToLoadInto, bool scaleToImageX, IRecieveBlenderByte scalingBlender, Stream stream)
{
if (scalingBlender == null)
{
scalingBlender = new BlenderBGRA();
}
ImageBuffer unScaledImage = new ImageBuffer(10, 10);
if (scaleToImageX)
{
// scale the loaded image to the size of the target image
AggContext.StaticData.LoadImageData(stream, unScaledImage);
// If the source image (the one we downloaded) is more than twice as big as our dest image.
while (unScaledImage.Width > imageToLoadInto.Width * 2)
{
// The image sampler we use is a 2x2 filter so we need to scale by a max of 1/2 if we want to get good results.
// So we scale as many times as we need to get the Image to be the right size.
// If this were going to be a non-uniform scale we could do the x and y separately to get better results.
ImageBuffer halfImage = new ImageBuffer(unScaledImage.Width / 2, unScaledImage.Height / 2, 32, scalingBlender);
halfImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, halfImage.Width / (double)unScaledImage.Width, halfImage.Height / (double)unScaledImage.Height);
unScaledImage = halfImage;
}
double finalScale = imageToLoadInto.Width / (double)unScaledImage.Width;
imageToLoadInto.Allocate(imageToLoadInto.Width, (int)(unScaledImage.Height * finalScale), imageToLoadInto.Width * (imageToLoadInto.BitDepth / 8), imageToLoadInto.BitDepth);
imageToLoadInto.NewGraphics2D().Render(unScaledImage, 0, 0, 0, finalScale, finalScale);
}
else
{
AggContext.StaticData.LoadImageData(stream, imageToLoadInto);
}
imageToLoadInto.MarkImageChanged();
}
/// <summary>
/// Download an image from the web into the specified ImageSequence
/// </summary>