tags:

views:

623

answers:

6

I'd like to create programmatically a random X height and Y width bitmap file.

The content, for me, is irrelevant. It could be all white, empty. What is important is the dimension.

How to do it using Windows API?

A: 

In .Net you can just new up a Bitmap object:

Image myImage = new Bitmap(width, height);
Max Schmeling
.NET isn't a choice, I'd like to do it using only the Windows API
Daniel Silveira
+1  A: 

The BMP format is extremely simple. You don't need special API aid here. Start here.

Eli Bendersky
A: 

You wish to create a Bitmap File using the windows API? There is no specific helper for this. a BMP file however is very simple:

  • Write out a BITMAPFILEHEADER struct.
  • Write out a BITMAPINFO struct.
  • Write out an array of bytes, enough to hold the format and dimensions described in the BITMAPINFO struct.

The MSDN has an Article with sample code demonstrating how.

Chris Becke
A: 

If you really insist on making an image with windows API,you should use Gdi32.dll In C# just call

Import dll file into your assembly, so you can use external methods in

DllImport["Gdi32.dll"]
HBITMAP CreateCompatibleBitmap(
  HDC hdc, 
  int nWidth, 
  int nHeight
); 

Then call Bitmap class from this Bitmap like

Bitmap bmp = Bitmap.FromHbitmap( nameOfBitmap );
bmp.Save("C:\NewImage.jpg");

There is an example in msdn page here

Myra
How would that solve his problem? He can't save a BMP file that way.
nikie
Creating bitmap is important in this question not saving !
Myra
A: 

GDI+ includes commands to load/save BMP images in C++. This sample code shows how to load and save images: Converting a BMP Image to a PNG Image. The Bitmap class also has a ctor that takes a width, a height and a pixel format to create empty images.

nikie
A: 

I suppose you have Microsoft .NET 2.0 Framework installed. (1.1 is also usable).

Using Notepad, create test.cs file with this code:

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 3)
                new System.Drawing.Bitmap(System.Convert.ToInt32(args[0]), System.Convert.ToInt32(args[1]))
                    .Save(args[2] + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            else
                System.Console.WriteLine("Usage: test.exe 100 200 filename");
        }
    }
}

Then create test.cmd file with this code:

@echo off
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe test.cs

Execute test.cmd

Execute text.exe

Viktor Jevdokimov