views:

15

answers:

2

Hi everyone. A bit of back story: I am currently trying to load a texture with OpenTK, so I am finding the next power of 2 (512,1024 etc) and creating a new bitmap with that size and drawing the original bitmap on:

Bitmap bmp = new Bitmap(filename);
width = bmp.Width;
height = bmp.Height;
int w2 = (int)PowerOf2(width);
int h2 = (int)PowerOf2(height);
Bitmap bmp2 = new Bitmap(w2, h2);
Graphics gfx = Graphics.FromImage(bmp2);
gfx.DrawImage(bmp, new Point(0,0));     
bmp = new Bitmap(w2, h2, gfx);
bmp.Save("save.bmp");

When I open the image though, it is all transparent. Does anyone know why this is?

A: 

When you do this:

bmp = new Bitmap(w2, h2, gfx);

you are not actually creating a copy of the bitmap that your Graphics object is using, just using its specifications to create a new empty one. Graphics directly manipulates a bitmap. You don't need to "get" it back from it. Get rid of that line and the one before, and save bmp2 instead, and it should work.

jamietre
A: 

On line 8, you draw bmp onto bmp2. Anything you do afterwards with gfx still affects bmp2.

On line 9, you created a new bitmap and assigned gfx to the bitmap, implying you'd draw onto bmp with gfx (which you didn't anyway). You then proceed to save bmp which gave you an empty, or as you describe it, "transparent" image.

If you're trying to double the size of your image, you might consider revising your code to something of this sort:

Private Sub DrawNewImage()
    Dim bmp = New Bitmap("C:\Path\to\file.bmp")
    Dim Width = bmp.Width
    Dim Height = bmp.Height
    Dim w2 As Integer = PowerOf2(Width)
    Dim h2 As Integer = PowerOf2(Height)

    Dim bmp2 = New Bitmap(w2, h2)
    Dim gfx = Graphics.FromImage(bmp2)

    gfx.DrawImage(bmp, 0, 0, w2, h2)
    bmp2.Save("C:\Path\to\saved\file.bmp")
End Sub

Private Function PowerOf2(ByVal value As Integer) As Integer
    Return value * value
End Function

or you can try the converted C# version:

private void DrawNewImage()
{
    Bitmap bmp = new Bitmap("C:\\Path\\to\\file.bmp");
    int Width = bmp.Width;
    int Height = bmp.Height;
    int w2 = PowerOf2(Width);
    int h2 = PowerOf2(Height);

    Bitmap bmp2 = new Bitmap(w2, h2);
    Graphics gfx = Graphics.FromImage(bmp2);

    gfx.DrawImage(bmp, 0, 0, w2, h2);
    bmp2.Save("C:\\Path\\to\\saved\\file.bmp");
}

private int PowerOf2(int value)
{
    return value * value;
}
Alex Essilfie
Thanks, I didn't know bmp2 and gfx would be linked
matio2matio