Comments (10)
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.
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.
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.
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.
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.
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.
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.
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.
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,
});
}
}
from imagesharp.
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.
@JimBobSquarePants
Thank you for helping me; the problem has been resolved.
from imagesharp.
@JimBobSquarePants
Thank you for helping me; the problem has been resolved.
from imagesharp.
Related Issues (20)
- CR2: System.NotSupportedException: Missing SOI marker offset for tiff with old jpeg compression HOT 2
- NullReferenceException when trying to load an animated PNG HOT 1
- Black Background after resizing in the latest version. HOT 4
- AccessViolationException and hard crash with animated webp HOT 8
- Simple conversion of Tiff file to PNG or JPG generates a strange result, even, at certain times, it is in 3D perspective HOT 6
- Please backport CVE-2024-27929 to 2.1.x HOT 20
- no vulnerability free version for netstandard or netframework 4.7 HOT 1
- Cannot update to v2.1.7 due to vulnerability tag HOT 4
- Exception SixLabors.ImageSharp.ImageFormatException: 'reserved bytes should be zero' when using Image.LoadAsync HOT 7
- Exif rotation and save HOT 4
- Error SixLabors.ImageSharp.ImageFormatException: 'Unexpected chunk followed VP8X header' HOT 3
- File (detected as bmp) fails to open, consumes a lot of memory HOT 4
- regression on latest.. 3 vs 2 with GIF HOT 6
- APNG Decoder incorrectly handles frame offsets and dispose previous with blend over HOT 3
- Hard crash (Internal CLR error) with corrupt PNG image HOT 3
- File conversion from jpg to webp changes colors of the image HOT 1
- Malicious hand-crafted PNG can be used to trigger DOS attack HOT 14
- I have encountered a bug with heigh and width of image HOT 4
- Missing Separable Blending Modes? HOT 2
- AccessViolationException when loading corrupt GIF
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagesharp.