why doesnt System.Drawing work on Console Application?  
Author Message
merwy





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

hi all

i wanna do a project with openGL in console application.but System.Drawing doesnt work.what must i do i'm waiting for ur replies.



.NET Development31  
 
 
The ZMan





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

[moving to .Net forum]

 
 
nobugz





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

A console window was designed to provide text output only and doesn't support any GDI or GDI+ calls. You need a real window, create one with a System.Windows.Forms.Form instance or the CreateWindow() API call.


 
 
merwy





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

thnx for ur reply.i'm using windows application any more.if u have code samples in C# with openGL,can u send me i'll be too glad.
 
 
nobugz





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

Lots of hits on searching just in these forums...


 
 
duffy399





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

I wanted to use System.Drawing.Bitmap and System.Drawing.Imaging.ImageFormat, i.e.

static void LoadThenSave(string dstPathAndBase, System.Drawing.Imaging.ImageFormat dstFmt, string srcPathAndFName) {

Bitmap myPat = new Bitmap(srcPathAndFName);

myPat.SetResolution(96, 96);

myPat.Save(dstPathAndBase + dstExt, dstFmt);

But I would also prefer to code a console application, so that if invoked from a command-line or in a batch file, any errors or messages would go to stdout, like traditional utilities. Is there any way to get System.Drawing to work in a Console Application

I did get my intended application working with a System.Windows.Forms.Form based application, but my System.Write calls only show in the Output window of VSE while Debugging. I never see the output from my System.Write calls when run at the Command Prompt.


 
 
nobugz





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

There's no problem working with bitmaps in a console application. You just need to add the right references. Project + Add reference, select System.Drawing. In your source code file:
using System.Drawing;

Your code needs work, you can't save the bitmap back like you do. GDI+ keeps a lock on the file. You'll have to make a copy and dispose the original. Here's an example:

using System;
using System.Drawing;

class Program {
static void Main(string[] args) {
try {
Bitmap bmp = new Bitmap(args[0]);
Bitmap cpy = (Bitmap)bmp.Clone();
bmp.Dispose();
cpy.SetResolution(96, 96);
cpy.Save(args[1]);
cpy.Dispose();
}
catch (Exception ex) {
Console.WriteLine("Conversion failed: {0}", ex.Message);
}
}
}



 
 
duffy399





PostPosted: .NET Base Class Library, why doesnt System.Drawing work on Console Application? Top

Yes, that worked great. Thanks. Now that I know that what I might be looking for is in that Add Reference scroll-list, I'll be able to do all sorts of things.

That is one mongo scroll-list though. I had actually gone so far as to activate that and scrolled part way down, before I posted. But it was so forbiddingly long, I gave up and cancelled before I got to System.Drawing.