ImageResizer Losing Transparency - c#

I am using ImageResizer from imageresizing.net and I am having a problem with transparency. When I resize png images, I am losing transparency and ending up with a black background.Here is my code, if anyone could help me it would be greatly appreciated.
var imagejob = new ImageJob();
imagejob.Instructions = new Instructions();
imagejob.Instructions.Width = 400;
imagejob.Instructions.Height = 300;
imagejob.Instructions.Mode = ImageResizer.FitMode.Max;
imagejob.Instructions.Format = "png";
imagejob.Instructions.OutputFormat = ImageResizer.OutputFormat.Png;
imagejob.Source = inStream;
imagejob.Dest = outStream;
ImageBuilder.Current.Build(imagejob);
FinalWidth = Convert.ToInt32(imagejob.FinalWidth);
FinalHeight = Convert.ToInt32(imagejob.FinalHeight);
BytesToSaveToDB = outStream.ToArray();

For anyone reading this, I have found the answer. The code i posted works fine and keeps transparency, I was using that code for resizing upon upload. I was actually then resizing the image again when displaying the image which i had not realised, and it was when displaying the image i was using the old code to resize which did not maintain transparency. So i was resizing twice and he old code was causing the issue, so the code i posted actually works fine.

Related

SVG rendering in C# with SVG library always fills shapes with black

I'm using the Svg.Core library (version 3.0.49.2) to render SVGs (defined in strings) to PNG images. No matter what I do, any shape seems to be rendered with a black stroke and a black fill.
Here's the code I'm using for a simple rectangle, as an example:
var svgString = #"<svg width=""300"" height=""300"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink""><rect x=""5"" y=""5"" height=""90"" width=""50"" fill=""#ef0000"" stroke=""#00ef00"" /></svg>";
var svgDocument = SvgDocument.FromSvg<SvgDocument>(svgString);
var bitmap = svgDocument.Draw();
bitmap.Save(fileName, ImageFormat.Png);
which ends up rendering a rectangle of the correct height and width, but all black:
I've seen a number of posts that mention various versions of inlining styles, but regardless of whether I'm using a style="" approach or a fill="", the problem continues. Also seems to happen without fill color specified or using standard color names instead of RGB values.
Any help or ideas are appreciated!
The best answer I have come up with here is from the comment earlier. If you put the following around it: svgDoc.Color = new SvgColourServer(Color.DarkGreen); svgDoc.StopColor = new SvgColourServer(Color.DarkGreen); svgDoc.Stroke = new SvgColourServer(Color.DarkGreen); svgDoc.Fill = new SvgColourServer(Color.DarkGreen); , you will get colors in the SVG. Posting this as the answer in case anyone runs into this issue down the road.

Resize transparent image with Magick.NET

I got some weird issues when resizing an image with transparency in Magick.NET. I am using Q16-AnyCPU.
I am resizing a 100px image to 400px.
MagickImage image = new MagickImage("test.png");
image.Resize(400, 400);
image.Write("test_resized.png");
I have tried many combinations of image.FilterType and image.Interpolate without any good results.
Only when I use image.AdaptiveResize(400, 400);, it looks somewhat better, but not as expected. The final image I want to resize is much bigger and AdaptiveResize is very slow.
When I disable Alpha via image.Alpha(AlphaOption.Off); I looks quice nice, but I want to keep the alpha.
Source image (the white area is transparent):
What I get:
What I want:
I had to set VirtuaPixelMethod, thanks for the hint:
MagickImage image = new MagickImage("test.png");
image.VirtualPixelMethod = VirtualPixelMethod.Transparent;
image.Resize(400, 400);
image.Write("test_resized.png");

WP8 MapOverlay Image Distortion

my Problem is, that when i put an image as the content of an MapOverlay on a Map,
the image starts to disappear if i zoom in close enough.
I created an image to illustrate the problem.
I recalculate the width and height of the image everytime i zoom, so it stays perfectly on the map. I checked it and the dimensions of the image are not the problem.
Does anybody can explain to me why this is happening?
Does anybody know how to fix this?
Thank you very much.
Unfortunately the windows SDK for HERE Maps has been deprecated and is no longer supported. It might be possible to achieve the requirement using the HERE JavaScript SDK 3.1 which supports better map overlays.
function addOverlayToMap(map) {
var imgCounter = 0;
// create an overlay that will use a weather map as a bitmap
var overlay = new H.map.Overlay(
new H.geo.Rect(
70.72849153520343, -24.085683364175395,
29.569664922291, 44.216452317817016
),
rainRadar[imgCounter],
{
// the bitmap is frequently updated mark the object as volatile
volatility: true
}
);
// update overlay's bitmap every 250 milliseconds
setInterval(function() {
imgCounter = imgCounter < 10 ? ++imgCounter : 0;
overlay.setBitmap(rainRadar[imgCounter]);
}, 250);
// add overlay to the map
map.addObject(overlay);
}
Reference : https://developer.here.com/documentation/examples/maps-js/geoshapes/image-overlay

Exporting a transparent TChart PNG distorts text

I'm using Steema TeeChart v4.1.2010.11303. I want to export a chart to a PNG image with a transparent background.
The resulting image has "distorted" or bold text in the left and bottom axis and in the header. It looks like being rendered with a raster font with a too low resolution. The legend on the right on the other hand looks fine:
The following sample code can be used to reproduce the problem:
TChart tChart = new TChart();
tChart.Aspect.View3D = false;
tChart.Panel.Brush.Gradient.Visible = false;
// Make the background of the chart transparent.
tChart.Panel.Transparent = true;
Steema.TeeChart.Styles.Bar series1 = new Steema.TeeChart.Styles.Bar( tChart.Chart );
series1.FillSampleValues();
tChart.Draw();
tChart.Graphics3D.BufferStyle = Steema.TeeChart.Drawing.BufferStyle.None;
using ( System.IO.Stream stream = new System.IO.MemoryStream() )
{
tChart.Export.Image.PNG.Width = m_PictureBox.Width;
tChart.Export.Image.PNG.Height = m_PictureBox.Height;
tChart.Export.Image.PNG.Save( stream );
// Show the bitmap in a Windows Forms PictureBox.
// Alternatively, it can also be saved in a file, which makes no difference.
PictureBox.Image = new Bitmap( stream );
}
When switching off the transparency with tChart.Panel.Transparent = false;, all text looks fine. However, I need a transparent background.
Is this a bug in TeeChart or am I missing something?
It is a know behavior for us and we haven't found a good solution for this problem, for the moment. If we will find a solution that we consider correct or we will arrive an interesting conclusion about problem of png export, we will inform you immediatly.
Thanks.
Best Regard,
Sandra Pazos

Capture a form to image

I have a form with controls, I need capture this form to image. Please help me. Thanks.
//Control cntrl; previously declared and populated
Bitmap bmp = new Bitmap(cntrl.Width,cntrl.Height);
cntrl.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
This works (almost) for me. I did note that MANY times the image would just be blank. When I searched online, it seems that many people were having this issue with the webBrowser control. It turns out that the initial image sometimes needs to be cleared to a solid color for this control. I really don't know why, but once it is initialized, I get much more consistent results. I've yet to get a blank image back.
Please note the code I've attached for clearing the initial image to all black.
try
{
string fn = Path.Combine(Path.GetTempPath(), "Error_screen.png");
Bitmap bmp = new Bitmap(internalBrowser.Width, internalBrowser.Height);
// In order to use DrawToBitmap, the image must have an INITIAL image.
// Not sure why. Perhaps it uses this initial image as a mask??? Dunno.
using (Graphics G = Graphics.FromImage(bmp))
{
G.Clear(Color.Black);
}
internalBrowser.DrawToBitmap(bmp, new Rectangle(0, 0,
internalBrowser.Width, internalBrowser.Height));
bmp.Save(fn, ImageFormat.Png);
}
catch
{
// handle exceptions here.
return "";
}
One VERY interesting side note (as compared to other solutions I've seen): This works for my control which is on a form that I have never shown! My form exists as a headless web browser, but this actual form has never seen the light of day.

Categories

Resources