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

Unable to generate a thumbnail as expected #2663

Closed
3 of 4 tasks
thinksea opened this issue Feb 13, 2024 · 10 comments
Closed
3 of 4 tasks

Unable to generate a thumbnail as expected #2663

thinksea opened this issue Feb 13, 2024 · 10 comments

Comments

@thinksea
Copy link

thinksea commented Feb 13, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

3.1.2

Other ImageSharp packages and versions

SkiaSharp.Views.Desktop.Common 2.88.7

Environment (Operating system, version and so on)

Microsoft Visual Studio Enterprise 2022

.NET Framework version

.NET 8.0

Description

Code example for generating thumbnail, result does not meet expectations

Steps to Reproduce

Unable to generate a thumbnail as expected

Images

webpDemo.zip

@tocsoft
Copy link
Member

tocsoft commented Feb 13, 2024

SkiaSharp.Views.Desktop.Common? we have nothing to do with SkiaSharp so can't really support you doing anything with that.

Don't expect people to download a random zip and run your project code without providing details on things like what are you trying to do, what are you expecting as an output etc... 'result does not meet expectations' can be very subjective depending on what your seeing.

@tocsoft
Copy link
Member

tocsoft commented Feb 13, 2024

Closing this as not enough details where provided, feel free to create a new issue once you can explain what the issue you are running into (without leaving us to do all the work to figure out what your problem is) and only where its relating explicitly to ImageSharp code in isolation from 3rd party libraries like SkiaSharp.

@tocsoft tocsoft closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2024
@thinksea
Copy link
Author

The issue occurred within ImageSharp.The introduction of "SkiaSharp" is solely for the purpose of comparing execution results.

The image triggered an exception. Without providing sample code, it is impossible to succinctly describe this exception.

Question 1: The thumbnail is not displayed centered.
Question 2: An exception was thrown directly while processing the second image.
Question 3: When resizing, the fill color does not support transparent colors.

@JimBobSquarePants
Copy link
Member

Without providing sample code, it is impossible to succinctly describe this exception.

I wouldn't say that is true. Your description lacks any detail at all. While example code is absolutely welcome, we need a proper description of actual vs expected results plus a detailed description of the issue.

To answer your questions though:

Question 1: The thumbnail is not displayed centered.

For which image? When I generate output for both using Resize the output is centered.

image

image

Question 2: An exception was thrown directly while processing the second image.

Yes, because you are mixing up your foreground and background arguments. SkiaSharp does a nearest neighbor resize of the image when passed a rectangle, but ImageSharp does not do that for you (we want you to control the quality of your output).

Your sample should have like the folowing.

using (var originalImage = Image.Load<Rgba32>(imageStream))
{
    using Image copy = new Image<Rgba32>(width, height, bgColor);
    copy.Mutate(x => x.DrawImage(originalImage, new Point(0, 866), 1));

    copy.SaveAsWebp(file.Substring(0, file.LastIndexOf('.')) + "_out_drawImage.webp", new SixLabors.ImageSharp.Formats.Webp.WebpEncoder()
    {
        Quality = imageQuality
    });
}

Now... we should protect better against this kind of mistake, and I have a simple fix in mind so will keep this issue open to track. that.

Question 3: When resizing, the fill color does not support transparent colors.

It does, however you're telling the library to decode the image using the encoded pixel format which in this case is Rgb24.

SkiaSharp will use a buffer by default that supports an alpha component, but we use the most efficient means unless told otherwise via using the generic Load<TPixel>(...) method.

using (var originalImage = Image.Load<Rgba32>(imageStream))

Note. It looks like you're confusing the pixel format also. ImageSharp pixel formats are arranged in least to most significant order (like DirectX) so [r, g, b, a] is Rgba32.

@thinksea
Copy link
Author

thinksea commented Feb 14, 2024

@JimBobSquarePants

Thank you for your assistance, which has cleared up most of my doubts. However, I still believe that when the caller uses "Rectangle," their intention is already quite clear, and they should be responsible for any consequences. The solution below has addressed my issue, although it may not be the optimal one.

using (var originalImage = SixLabors.ImageSharp.Image.Load<SixLabors.ImageSharp.PixelFormats.Rgba32>(imageStream))
{
	using (var copy = new Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(width, height))
	{
		copy.Mutate(x => x.BackgroundColor(bgColor));

		var rect = new SixLabors.ImageSharp.Rectangle(0, 866, 200, 267);

		//copy.Mutate(x => x.DrawImage(originalImage, rect, 1)); // original code

		#region new code
		var resizeOptions = new ResizeOptions
		{
			//Position = AnchorPositionMode.Center,
			Size = new SixLabors.ImageSharp.Size(rect.Width, rect.Height),
			Mode = ResizeMode.Stretch,
			//PadColor = SixLabors.ImageSharp.Color.Transparent,
		};
		originalImage.Mutate(x => x.Resize(resizeOptions));
		copy.Mutate(x => x.DrawImage(originalImage, new SixLabors.ImageSharp.Point(rect.X, rect.Y), 1));
		#endregion

		copy.SaveAsWebp(file.Substring(0, file.LastIndexOf('.')) + "_out_drawImage.webp", new SixLabors.ImageSharp.Formats.Webp.WebpEncoder()
		{
			Quality = imageQuality,
		});
	}
}

@JimBobSquarePants
Copy link
Member

I'm glad I could help.

However, I still believe that when the caller uses "Rectangle," their intention is already quite clear, and they should be responsible for any consequences.

It doesn't matter what a user's intentions are. The foreground and background rectangle parameters are designed for determining what portion of the foreground image to draw and where on the background to draw it, not to provide instructions to scale an image.

Compare the output of the two operations. Skia on the left produces incredibly poor output quality at ~10x the file size by making assumptions. The quality of the output matters to me and I want my users to have complete control over what they produce.

image

Here's your code rewritten in a more succinct manner. I don't know about you, but I believe that to be a very reasonable amount of code to perform a complex job.

    // Namespace includes. 
    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.Formats.Webp;
    using SixLabors.ImageSharp.PixelFormats;
    using SixLabors.ImageSharp.Processing;

    // Method.
    using Image foreground = Image.Load(imageStream);
    using Image<Rgba32> background = new(width, height, bgColor);

    foreground.Mutate(x => x.Resize(new ResizeOptions()
    {
        Size = new Size(200, 267),
        Mode = ResizeMode.Stretch,
    }));

    background.Mutate(x => x.DrawImage(foreground, new Point(0, 866), 1f));
    background.SaveAsWebp(
        Path.GetFileNameWithoutExtension(file) + "_out_drawImage.webp",
        new WebpEncoder()
        {
            Quality = imageQuality,
        });

@thinksea
Copy link
Author

Question 1: The thumbnail is not displayed centered.

The code is like this:

using (var originalImage = SixLabors.ImageSharp.Image.Load<SixLabors.ImageSharp.PixelFormats.Rgba32>(imageStream))
{
	using (var copy = new Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(200, 2000))
	{
		copy.Mutate(x => x.BackgroundColor(bgColor));

		var rect = new SixLabors.ImageSharp.Rectangle(0, 866, 200, 267);

		copy.Mutate(x => x.DrawImage(originalImage, rect, 1));

		copy.SaveAsWebp(file.Substring(0, file.LastIndexOf('.')) + "_out_drawImage.webp", new SixLabors.ImageSharp.Formats.Webp.WebpEncoder()
		{
			Quality = 100,
		});
	}
}

Original Image:
jpg1

Expected Outcome:
Expected Outcome

Actual Outcome:
Actual Outcome

@JimBobSquarePants
Copy link
Member

I wouldn't have expected that outcome given that we do not scale the foreground image and you are passing the foreground rectangle. The output here is doing exactly what you asked it to do.

@thinksea
Copy link
Author

@JimBobSquarePants
Thank you for helping me; the problem has been resolved.

1 similar comment
@thinksea
Copy link
Author

@JimBobSquarePants
Thank you for helping me; the problem has been resolved.

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

No branches or pull requests

3 participants