Rory Becker
Rory Becker

Reputation: 15721

How do I create an image based on the composite of 2 pngs each with transparent areas

I have 3 images.

  1. GreenBackground.png
  2. RedCircle.png
  3. BlueCircle.png

They are all 80x60 pictures

The red and Blue circles appear at different locations within this area.

I'm trying to produce a composite image by loading first the background, then the red circle and finally the blue circle.

However the resultant Results.png image appears, in explorer, to be entirely black, and when opened in windows photo viewer, it appears to be entirely transparent.

    Dim Background = Image.FromFile("GreenBackground.png", True)
    Dim RedCircle = Image.FromFile("RedCircle.png", True)
    Dim BlueCircle = Image.FromFile("BlueCircle.png", True)

    Dim Canvas = Graphics.FromImage(Background)
    Canvas.DrawImage(RedCircle, New Point(0, 0))
    Canvas.DrawImage(BlueCircle, New Point(0, 0))
    Dim Result As New Bitmap(Background.Width, Background.Height, Canvas)
    Result.Save("Result.png", System.Drawing.Imaging.ImageFormat.Png)

Any ideas what I might be doing wrong?

Upvotes: 0

Views: 585

Answers (1)

Joshua
Joshua

Reputation: 331

The Bitmap constructor you are using only uses the DPI of the passed graphics object:

The Graphics object that specifies the resolution for the new Bitmap. MSDN Ref

So basically you are creating a new empty bitmap at the moment. Your graphics object is set up to modify the Background image, so all you need to do is:

Background.Save("Result.png", System.Drawing.Imaging.ImageFormat.Png) at the end of the modifications.

LinqPad Example:

Sub Main
    Dim image = System.Drawing.Image.FromFile("y.png", true)
    Dim overlay = System.Drawing.Image.FromFile("x.png", true)
    Dim g = System.Drawing.Graphics.FromImage(image)
    g.DrawImage(overlay, new System.Drawing.Point(0,0))
    image.Save("result.png", System.Drawing.Imaging.ImageFormat.Png)
End Sub

Upvotes: 1

Related Questions