-
-
Notifications
You must be signed in to change notification settings - Fork 851
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
Comments
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. |
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. |
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. |
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:
For which image? When I generate output for both using Resize the output is centered.
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.
It does, however you're telling the library to decode the image using the encoded pixel format which in this case is 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 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 |
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.
|
I'm glad I could help.
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. 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,
}); |
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,
});
}
} |
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. |
@JimBobSquarePants |
1 similar comment
@JimBobSquarePants |
Prerequisites
DEBUG
andRELEASE
modeImageSharp 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
The text was updated successfully, but these errors were encountered: