Png image processing in .NET.
Posted
by
Oybek
on Stack Overflow
See other posts from Stack Overflow
or by Oybek
Published on 2012-10-07T15:36:13Z
Indexed on
2012/10/07
15:36 UTC
Read the original article
Hit count: 305
I have the following task. Take a base image and overlay on it another one. The base image is 8b png as well as overlay. Here are the base (left) and overlay (right) images.
Here is a result and how it must look.
The picture in the left is a screenshot when one picture is on top of another (html and positioning) and the second is the result of programmatic merging.
As you can see in the screenshot the borders of the text is darker. Also here are the sizes of the images
- Base image 14.9 KB
- Overlay image 6.87 KB
- Result image 34.8 KB
The size of the resulting image is also huge
Here is my code that I use to merge those pictures
/*...*/
public Stream Concatinate(Stream baseStream, params Stream[] overlayStreams) {
var @base = Image.FromStream(baseStream);
var canvas = new Bitmap(@base.Width, @base.Height);
using (var g = canvas.ToGraphics()) {
g.DrawImage(@base, 0, 0);
foreach (var item in overlayStreams) {
using (var overlayImage = Image.FromStream(item)) {
try {
Overlay(@base, overlayImage, g);
} catch {
}
}
}
}
var ms = new MemoryStream();
canvas.Save(ms, ImageFormat.Png);
canvas.Dispose();
@base.Dispose();
return ms;
}
/*...*/
/*Tograpics extension*/
public static Graphics ToGraphics(this Image image,
CompositingQuality compositingQuality = CompositingQuality.HighQuality,
SmoothingMode smoothingMode = SmoothingMode.HighQuality,
InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic) {
var g = Graphics.FromImage(image);
g.CompositingQuality = compositingQuality;
g.SmoothingMode = smoothingMode;
g.InterpolationMode = interpolationMode;
return g;
}
My questions are
- What should I do in order to merge images to achieve the result as in the screenshot?
- How can I lower the size of the result image?
- Is the
System.Drawing
a suitable tool for this or is there any better tool for working with png for .NET?
© Stack Overflow or respective owner