Я создал консольное приложение .net core 2.0, в котором я загружаю изображения с разных URL-адресов и изменяю их размер до разрешения 2, а затем сохраняю их в BLOB-объекты Azure.
Теперь большую часть времени я получаю тайм-аутисключение даже для рабочих изображений.(проверил URL в браузере и нашел его работающим).Я изо всех сил пытаюсь выяснить причину и решение.Я также заметил, что при обработке изображений загрузка процессора достигает 100%.Ниже мой код.
Task[] requests = xmlProperties.Select(l => UploadImage(l.PropertyRefNo, l.Images)).ToArray();
await Task.WhenAll(requests);
public async Task UploadImage(string PropertyRefNo, List<string> Images)
{
Images = Images.OrderBy(x => x).Take(10).ToList();
var azureStorageConfig = "DefaultEndpointsProtocol=xxxx";
var containerReference = "xxx";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(@azureStorageConfig);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerReference);
var d = await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
var index1 = 0;
foreach (var item in Images)
{
try
{
index1 = index1 + 1;
using (MemoryStream imageStream = ImageUtilities.GetImageData(item))
{
if (index1 >= 0 && index1 < 4)
{
using (var resized = ImageUtilities.ResizeImageWithSameAspectRation(imageStream, 262, 230))
{
if (resized != null)
{
string blobName = PropertyRefNo + "/" + PropertyRefNo + "_" + index1 + ".jpeg";
blobName = blobName.Replace("#", "-");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
byte[] byteArray;
using (MemoryStream memoryStream = new MemoryStream())
{
resized.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byteArray = memoryStream.ToArray();
}
using (var ms = new MemoryStream(byteArray, writable: false))
{
await blockBlob.UploadFromStreamAsync(ms);
Console.WriteLine("Icon: " + blobName);
}
}
}
}
using (var resized = ImageUtilities.ResizeImageWithSameAspectRation(imageStream, 800, 600))
{
if (resized != null)
{
string blobName = PropertyRefNo + "/" + PropertyRefNo + "_full_" + index1 + ".jpeg";
blobName = blobName.Replace("#", "-");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
byte[] byteArray;
using (MemoryStream memoryStream = new MemoryStream())
{
resized.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byteArray = memoryStream.ToArray();
}
using (var ms = new MemoryStream(byteArray, writable: false))
{
await blockBlob.UploadFromStreamAsync(ms);
Console.WriteLine("Full: " + blobName);
}
}
}
}
}
catch (Exception ex)
{
await AddLog(0, JobStatus.ImageFailedFull.ToString(), ex.ToString(), item, 0);
}
}
}
public class WebClientWithTimeout : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest wr = base.GetWebRequest(address);
wr.Timeout = 100000; // 100 seconds
return wr;
}
}
public static Bitmap ResizeImageWithSameAspectRation(String filename, int maxWidth, int maxHeight)
{
try
{
WebClient wc = new WebClientWithTimeout();
byte[] imageStream = wc.DownloadData(filename);
MemoryStream ms = new MemoryStream(imageStream);
//System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
using (Image originalImage = Image.FromStream(ms))
{
//Caluate new Size
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
//--Quality Settings Adjust to fit your application
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
}
}
catch (Exception ex)
{
throw ex;
}
}