Giter Club home page Giter Club logo

Comments (10)

JimBobSquarePants avatar JimBobSquarePants commented on June 18, 2024 1

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,
        });

from imagesharp.

tocsoft avatar tocsoft commented on June 18, 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.

from imagesharp.

tocsoft avatar tocsoft commented on June 18, 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.

from imagesharp.

thinksea avatar thinksea commented on June 18, 2024

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.

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on June 18, 2024

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.

from imagesharp.

thinksea avatar thinksea commented on June 18, 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,
		});
	}
}

from imagesharp.

thinksea avatar thinksea commented on June 18, 2024

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

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on June 18, 2024

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.

from imagesharp.

thinksea avatar thinksea commented on June 18, 2024

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

from imagesharp.

thinksea avatar thinksea commented on June 18, 2024

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

from imagesharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.