Ugly rendering of image with DrawString-Method (ASP.NET) - asp.net

I want to render some text as image in ASP.NET. It is working well, but the text is rendered very ugly (with grey pixels around the text) even if I turn on AntiAlias (or ClearType) by changing the graphics TextRenderingHint option.
Here is the relevant code:
float width;
float height;
System.Drawing.Text.PrivateFontCollection fontcollection = new System.Drawing.Text.PrivateFontCollection();
// Add the custom font families
fontcollection.AddFontFile(Server.MapPath("./Fonts/" + fontfile));
Bitmap image = new Bitmap(10, 10);
Graphics graphic = Graphics.FromImage(image);
Font font = new Font(fontcollection.Families.First(), fontsize, style);
SizeF size = graphic.MeasureString(text, font);
width = size.Width;
height = size.Height;
image = new Bitmap(Convert.ToInt32(width), Convert.ToInt32(height));
graphic = Graphics.FromImage(image);
graphic.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphic.DrawString(text, font, Brushes.Black, new PointF(0, 0));
Response.ContentType = "image/jpeg";
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Here a link to the generated image (zoom):
Unzoomed image:
How can I solve this?

Make the imageformat PNG.
The default JPEG compression is useless.

Related

Enlarge Image Canvas

Is there a quick way using System.Drawing to quickly enlarge the image canvas of an .png image? (see example below). The caveat is the background might be transparent and I want to keep it transparent.
Edit: Needs to be in ASP .Net CORE
Alternatively, is there a way of putting the image on a white background that is slightly larger than the image?
After a few days of trial and error, I think I found something that works
Image overlayImage = //get your image here from file or url.
xloc = //x coord where to start overlay image.
yloc = //y coord where to start overlay image.
canvasWidth = //width of background canvas
canvasHeight = //height of background canvas
Bitmap baseImage = new Bitmap(canvasWidth, canvasHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(baseImage))
{
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White))
{
graphics.FillRectangle(myBrush, new Rectangle(0, 0, canvasWidth, canvasHeight)); // white rectangle
}
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(overlayImage, xloc, yloc);
} // graphics will be disposed at this line

Resize existing JavaFX image

I have to load images:
Image image = new Image(f.toURI().toString(), width, height, true, true);
Since I also have tiffs, I have to load them differently using JAI:
BufferedImage read = ImageIO.read(f);
Image image = SwingFXUtils.toFXImage(read, null);
Now I have an Image object, but it has the wrong size. Image offers no methods to resize the object. How do I resize is so it has the same size as if I would load a jpg or png with the shown line?
Seems like there should be an easier way to do this, but you can try:
BufferedImage read = ImageIO.read(f);
int[] pixels = new int[width * height] ;
PixelGrabber grabber = new PixelGrabber(read.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, width, height, 0, pixels, width);
grabber.grabPixels();
WritableImage fxImage = new WritableImage(width, height);
PixelWriter pw = fxImage.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);
Now fxImage is a javafx.scene.image.Image that has dimensions width by height.
Be aware that PixelGrabber.grabPixels() is a blocking call, so you will need to handle InterruptedExceptions. For large images you might want to do this in a Task executed on a background thread, so as not to block the FX Application Thread.

HTML5 Canvas with Predefined Image

Recently I started working on HTML5 Canvas, I'm new to it.
I've a problem as follows:
I'm loading a Canvas with Body Chart Image (Predefined Image) and on that User will Draw some lines, shapes, etc.
After that I'll generate an image object as follows
var canvas = document.getElementById("MyCanvas");
var dataURL = canvas.toDataURL();
var image = new Image();
image.src = dataURL;
But, Here it generates only those elements which are drawn by users (lines, shapes) as PNG Image. It won't take that Predefined canvas background Image.
I need to generate a PNG image which should include both the Canvas background Image as well as User entered drawing elements.
How to do this?
Try to actually draw you image onto your canvas, utilizing these functions:
var canvas = document.getElementById("MyCanvas");
var img = new Image();
img.src = 'pathToYourImageHere';
canvas.drawImage(img,0,0); /* 0,0 is x and y from the top left */
When you now try to save it, it should also save your background image.
EDIT:
In response to your comment:
You can circument your layering problem by using two different canvases. One for the image, and one for your drawing. Then layer them on top of each other using absolute positioning.
You can read more here: Save many canvas element as image
EDIT2:
But actually you shouldn't have a layering problem, since the following code will first draw the image and then draw the arc, and the layering will be fine:
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var imageObj = new Image();
imageObj.src = "somePathToAnImage";
context.drawImage(imageObj, 50, 50);
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = 1.1 * Math.PI;
var endAngle = 1.9 * Math.PI;
var counterClockwise = false;
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
context.lineWidth = 15;
// line color
context.strokeStyle = "red";
context.stroke();
Even though the layering is fine, you will be better of by using two canvases, in case you would like to only save the drawing, without the background. You can always save both into a new canvas and save that, when you only use one canvas you'll have a hard time separating the drawing from the background.
This is because the image needs time to load, you have to use the onload function.
imgObj.onload = function() { context.drawImage(imageObj, 50, 50); imgLoaded = true;}
if (imgLoaded) { /*you draw shapes here */ }

Image lost transparent effect when stored in Table

I want store the image in SQL server table, and I successed but when I retrive that image on page, I found that the image has lost its transparency. The origional image is png/gif. I have resized that image in 100px / 100px.
I have used following code to resixe the image. It works but when it stores image in database it lost the transparency.
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
System.Drawing.Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (System.Drawing.Graphics canvas = System.Drawing.Graphics.FromImage(newImage))
{
canvas.Clear(System.Drawing.Color.Transparent);
canvas.SmoothingMode = SmoothingMode.HighQuality;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Png);
return m.GetBuffer();
}
}
}
Any solution????
Thanks
This one probably answers your question:
Why does resizing a png image lose transparency?
As the above link suggests, you're not loosing transparency because the image is saved into the database. It's when you resize the image that the transparency is lost.

How to make transparent color in BitmapData in flex

I have a code like this:
var bitmapData:BitmapData = new BitmapData(width, height);
bitmapData.draw(this);
var ba:ByteArray = (new PNGEncoder()).encodeByteArray(bitmapData.getPixels(clipRect),width,height,true);
I want to make the white color in the generated PNG transparent. What is the best way to do so?
You were close :)
You need to tell the bitmapData to be transparent, and then fill it with all transparent pixels.
var bitmapData:BitmapData = new BitmapData(width, height, true, 0);
(the 0 is shorthand for 0x00000000 which is a 32 bit ARGB color expressed as a uint)

Resources