Lars Holdgaard
Lars Holdgaard

Reputation: 9966

Dynamically create a picture with some texts and background image as PNG

I have the following:

I need to set these dynamically together in an image, and then store it as a PNG file. All of this happens in a web service which sends some kind of stream back.

I would love to be able to style this with CSS, as it would be nice to add some CSS effects to the picture. If that is not possible, it is fine as well.

So far I've thought about using the Bitmap class, and then insert my things dynamically. This is definetely one way to go, but I would really prefer some more design-friendly way like CSS.

What is the prefered way to do this? Any experiences?

Upvotes: 0

Views: 1240

Answers (2)

Rune Grimstad
Rune Grimstad

Reputation: 36300

The answer from Angelo Geels will let you create a dynamic bitmap in your code. If you need more flexibility then you might want to use the SVG format instead, and possibly render it as a PNG file if you absolutely need that format.

SVG is an XML based image format where you define a viewport and shapes that should be drawn into it. It is vector based so the images can be scaled up and down to any size.

It should be quite easy to write an ASP.Net Http Handler that outputs an SVG xml file with some dynamic content based on user input. An even easier option would be to serve static SVG files that your designers create, but then you might as well serve static PNG files.

If you want to use the SVG approach for its flexibility but still need to output PNG's then you can use the SVG.Net library to read the SVG file and then use the SvgDocument.Draw method to draw the image to a Bitmap object that can then be written out as a PNG file.

Upvotes: 1

Codecat
Codecat

Reputation: 2241

Check out System.Drawing.Graphics.

Bitmap bmp = new Bitmap(500, 500);
using (Graphics g = Graphics.FromImage(bmp)) {
  g.Clear(Color.Black);
  // ...
}

You can then call Image.Save (bmp.Save(...)) to save the image to disk or even to a MemoryStream.

Upvotes: 2

Related Questions