Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Resize method rotating image during resize for one particular image #923

Open
abubkdar opened this issue Aug 1, 2019 · 3 comments
Projects

Comments

@abubkdar
Copy link

abubkdar commented Aug 1, 2019

Description
I am resizing image with SKBitmap.Resize method for one particular image , its resizing and rotating to 180 degree as well.

resizing to ( height , width)
200 x 200
50 x 50
1000 x 1000

Code

public Stream GetResizedImage(Stream image, ImageMaxSize size, string imageFormat)
{
	image.Position = 0L;

	using (var inputStream = new SKManagedStream(image))
	using (var original = SKBitmap.Decode(inputStream))
	{
		if (original.ColorType == SKColorType.Gray8 || original.ColorType == SKColorType.Unknown)
		{
			original.CopyTo(original, SKImageInfo.PlatformColorType);
		}
                
               
		var (width, height) = ResizeKeepAspect(original.Width, original.Height, size.MaxWidth, size.MaxHeight);
		using (var resized = original.Resize(new SKImageInfo(width, height, original.ColorType, original.AlphaType,original.ColorSpace),SKFilterQuality.High))
		{
			if (resized == null)
			{
				_logger.LogWarning("Failed resizing image to width {0} and height {1}; color type: {2}", width, height, original.ColorType);
				return null;
			}

			using (var newImage = SKImage.FromBitmap(resized))
			{
				return newImage.Encode(GetImageFormat(imageFormat), 85).AsStream();
			}
		}
	}
}

Expected Behavior

Image should be resized only

Actual Behavior

Images resized & rotated to 180 degree as well

Basic Information

  • Version with issue: 1.68.0
  • Last known good version:
  • IDE: Visual studio 2017
  • Platform Target Frameworks: .net core
    • Android:
    • iOS:
    • Linux:
    • macOS:
    • Tizen:
    • tvOS:
    • UWP:
    • watchOS:
    • Windows Classic:
  • Target Devices:

Screenshots

Reproduction Link

@abubkdar
Copy link
Author

abubkdar commented Aug 1, 2019

Further If I save as same image as .PNG , Now I can resize this PNG without issue

@Redth Redth added this to Needs Triage in Triage Aug 2, 2019
@handcraftedsource
Copy link

I can reproduce this with all images with portrait orientation. Images with Landscape orientation remain correctly aligned.

My code looks like this:

public static async Task<string?> GetResizedPngImageAsBase64String(this System.IO.Stream inputStream, long streamSize, int maxHeight, int maxWidth)
{
    var inputBuffer = new byte[streamSize];
    await inputStream.ReadAsync(inputBuffer);
    using var original = SKBitmap.Decode(inputBuffer);

    var scaleHeight = maxHeight / (float) original.Height;
    var scaleWidth = maxWidth / (float) original.Width;
    var scale = Math.Min(scaleHeight, scaleWidth);

    var width = (int)(original.Width * scale);
    var height = (int)(original.Height * scale);
    
    using var resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High);
    if (resized == null)
    {
        return null;
    }

    using var image = SKImage.FromBitmap(resized);
    var buffer = image.Encode(SKEncodedImageFormat.Png, 100).ToArray();
    var result = Convert.ToBase64String(buffer);
    return result;
}

Used Package Version: 2.80.3
Framework: net6.0

JoyfulReaper added a commit to JoyfulReaper/PlantTracker that referenced this issue Jul 30, 2022
@wolfgangschneider
Copy link

HI
I run into the same issue. Is this Bug still open ? Is there a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Triage
  
Needs Triage
Development

No branches or pull requests

3 participants