Answers for "resize image async imagesharp"

C#
0

resize image async imagesharp

public async static Task<bool> CropImageAsync(string sourceim, int destinationWidth, int destinationHeight)
    {
        if (sourceim.EndsWith(".png"))
        {
            using (var fullSizeStream = new MemoryStream())
            using (var smallStream = new MemoryStream())
            using (var thumbStream = new MemoryStream())
            using (var reviewThumbStream = new MemoryStream())
            using (var image = SixLabors.ImageSharp.Image.Load(sourceim))
            {
                var img = image.Clone(context => context
                    .Resize(new ResizeOptions
                    {
                        Mode = ResizeMode.Stretch,
                        Size = new SixLabors.ImageSharp.Size(destinationWidth, destinationHeight)
                    }));
                try
                {
                    await img.SaveAsPngAsync(sourceim);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
        else if (sourceim.EndsWith(".jpeg") || sourceim.EndsWith(".jpg"))
        {
            using (var fullSizeStream = new MemoryStream())
            using (var smallStream = new MemoryStream())
            using (var thumbStream = new MemoryStream())
            using (var reviewThumbStream = new MemoryStream())
            using (var image = SixLabors.ImageSharp.Image.Load(sourceim))
            {
                var img = image.Clone(context => context
                    .Resize(new ResizeOptions
                    {
                        Mode = ResizeMode.Stretch,
                        Size = new SixLabors.ImageSharp.Size(destinationWidth, destinationHeight)
                    }));
                try
                {
                    await img.SaveAsJpegAsync(sourceim);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
        else if (sourceim.EndsWith(".gif"))
        {
            using (var fullSizeStream = new MemoryStream())
            using (var smallStream = new MemoryStream())
            using (var thumbStream = new MemoryStream())
            using (var reviewThumbStream = new MemoryStream())
            using (var image = SixLabors.ImageSharp.Image.Load(sourceim))
            {
                var img = image.Clone(context => context
                    .Resize(new ResizeOptions
                    {
                        Mode = ResizeMode.Stretch,
                        Size = new SixLabors.ImageSharp.Size(destinationWidth, destinationHeight)
                    }));
                try
                {
                    await img.SaveAsGifAsync(sourceim);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
        else
        {
            return false;
        }
    }
Posted by: Guest on March-26-2021

C# Answers by Framework

Browse Popular Code Answers by Language