-1

I am doing a console application in c#. In this app, I have to create a bitmap of png type and it has to to be stored in some defined path (say C: or D: drive).

In a windows application I have the below code to create a bitmap and it will be shown in a picture box.

void CreateBitmap()
{
    System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
    for( int x = 0; x < flag.Height; ++x )
      for( int y = 0; y < flag.Width; ++y )
        flag.SetPixel(x, y, Color.White);
      for( int x = 0; x < flag.Height; ++x )
        flag.SetPixel(x, x, Color.Red);
    pictureBox1.Image = flag;
}

How can I create and store this in a specified path using a console application?

I have changed my code as below but still error exists:

static void CreatePng(string[] binvalues)
{
    String aName = System.Reflection.Assembly.GetExecutingAssembly().Location;
    String aPath = System.IO.Path.GetDirectoryName(aName);
    string[] ExecDirectories = System.IO.Directory.GetDirectories(aPath);

    System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);

    for (int x = 0; x < flag.Height; ++x)
        for (int y = 0; y < flag.Width; ++y)
            flag.SetPixel(x, y, Color.White);
    for (int x = 0; x < flag.Height; ++x)
        flag.SetPixel(x, x, Color.Red);
    flag.Save(aPath, System.Drawing.Imaging.ImageFormat.Png);
}

It is showing run-time error in last line where flag.save seems something wrong?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
tech_guy
  • 13
  • 1
  • 4
  • 1
    http://www.whathaveyoutried.com/ – Maarten Nov 01 '12 at 14:51
  • 1
    @Marteen: You could at least make the effort to package that link into a nice comment...while I agree with the link and it's purpose, your comment sucks and [is not really helpful](http://meta.stackexchange.com/questions/147161/since-when-is-http-whathaveyoutried-com-banned). – Bobby Nov 30 '12 at 08:56

4 Answers4

1

Use the same code, and instead of assigning to a PictureBox, call the Save() method on the bitmap:

flag.Save("yourpath", System.Drawing.Imaging.ImageFormat.Png);

Note that you may have to add a reference to System.Drawing in your console application, as it is not there by default.

Jon B
  • 51,025
  • 31
  • 133
  • 161
0

Now, I've never done this myself using C# so someone more inclined could possibly help but here's what I was able to find out.

1) You need to have your image data stored in a Bitmap. From the looks of things you're doing this already with flag

2) You need to call the save() function on the Bitmap:

flag.Save(filename, ImageFormat.Png);

3) filename would be a String that you define. This is fairly easy as you could simply have the application prompt the user to enter a path and save it.

Questions?

Disclaimer: I received my info from this page. There is a lot of discussion on the "proper" way to save a png you can dig from.

Community
  • 1
  • 1
Grambot
  • 4,370
  • 5
  • 28
  • 43
  • Can you post the runtime error for us? There should be a stack trace detailing exactly what's wrong.... might be missing directory, bad format, or file not found? Something else? – Grambot Nov 01 '12 at 15:51
  • The Error thrown is : " An unhandled Exception of type 'System.Runtime.Interopservices.ExternalException' occurred in System.Drawing.dll" An Additional information : A generic error occurred in GDI+ – tech_guy Nov 01 '12 at 17:00
  • Are you saving it to a new file or overwriting an old one? You'd get an exception like that if you were trying to save over a file that is in use. EDIT: Its also possible that the path you're saving to doesn't exist. Are you checking to make sure the path is valid? – Grambot Nov 01 '12 at 17:36
  • the filename is not used while running., its a new onebut donno., still throws an error – tech_guy Nov 01 '12 at 18:21
  • Looking at your code again, you're saving to `aPath` but no file name? try `flag.Save(aPath + "image.png", ImageFormat.Png);` and see if that helps. – Grambot Nov 01 '12 at 18:56
0

Use this:

flag.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
  1. You don't need to add the image to a PictureBox. You have a console application and PictureBox is a visual control.
  2. For future questions it would be nice if you spent a minimum amount of time of searching SO or other sources for an answer first
  3. The question is not tagged correctly. If you want to save a System.Drawing.Bitmap it can not be tagged BitmapImage as this is for WPF only. I changed this.
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

You can try below code-:

public bool ResizeImage(string OriginalFilepath, string NewFilepath)
{
  Bitmap original = (Bitmap)Image.FromFile(OriginalFilepath);
  Bitmap resized = new Bitmap(original, new Size(Width,Height));
  resized.Save(NewFilepath.png);      
}
McDowell
  • 107,573
  • 31
  • 204
  • 267