How exactly do I add images to a console application?

Recommended Answers

All 12 Replies

Adding them is the easy bit. Doing something with them is harder!

First step is to create a resource script, which is really just a list of the images you want in your app. It looks something like this:

IMAGE1 BITMAP CHEMICAL.BMP
IMAGE2 BITMAP FACTORY.BMP
IMAGE3 BITMAP FINANCE.BMP
IMAGE4 BITMAP HANDSHAK.BMP
IMAGE5 BITMAP SHIPPING.BMP

Save the file. It is normal to use a .RC extension so you might call it Bitmaps.RC

Next compile that script to produce a resource file. At the command line type

BRCC32 Bitmaps.RC

This will produce a file called Bitmaps.RES

Add a compiler directive at the start of your app to include that resource file, like this:

{$R Bitmaps.res}

Now when you build your app the bitmaps are included as resources. (You can see the effect on the file size, for example).

After that you can load the bitmaps in the usual way. For example:

procedure ShowImage(ImageName : string);
var
  Bmp : VCL.Graphics.TBitmap;
begin
  Bmp := VCL.Graphics.TBitmap.Create;
  try
    Bmp.LoadFromResourceName(hInstance, ImageName);
    writeln(format('%s is %d wide x %d high',[ImageName, Bmp.Width, Bmp.Height]));
  finally
    Bmp.Free;
  end;
end;

begin
  ShowImageSize('IMAGE2');
end;

I am sorry, but I have never really done anything like this before. How exactly do I create a resource script?

I managed to make the resource script and everything else, but I am getting the error...

Undeclared identifier VCL.

Along with a lot more problems, they mostly seem to stem from this though.

Just use a simple editor like Notepad to create the .rc file. Sorry, I should have said that. (You could even edit it in the Delphi editor if you wanted to do it that way.)

What version of Delphi are you running?
If you have an older version you should USE Graphics instead of VCL.Graphics and just put TBitmap instead of VCL.Graphics.TBitmap.

Oh one caveat is that if you are running an older version where you just put Graphics instead of VCL.Graphics etc, there is the possibility of namespace ambiguity causing compilation errors. It can be sensitive to the order in which units appear in your uses clause and you may need to change their order to get your code to compile. In particular if you are using both the Windows and Graphics units you may need to swap the order of those as they both define TBitmap and if you get the wrong one you'll have problems.

I am sorry, I have no idea what I am doing. Here is my code.

    program TICTACTOEVISUAL;
    {$APPTYPE CONSOLE}
        {$R '\\VIKING-FS\Students\2015\SchroAus\Pascal\Tic\pictures.res' '\\VIKING-FS\Students\2015\SchroAus\Pascal\Tic\pictures.res'}
    uses
  SysUtils,
  Windows,
  MMSystem;

const
    crossbar = '---+---+---';
    verticalbar = '|';

type
       Mark = (EMPTY, CROSS, ZERO);

var
       Board: array[1..3, 1..3] of Mark;
           Again: Char;
           Play:Boolean;
           sbi: TConsoleScreenBufferInfo;
           i: integer;
Procedure ClrScrn;
BEGIN
GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),sbi);
GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),
sbi);
for i:= 0 to sbi.dwSize.y do
writeln;
end;


procedure initBoard;
       var
          i, j: integer;
       begin
          for i := 1 to 3 do
          begin
             for j := 1 to 3 do
             begin
                board[i,j] := EMPTY;
             end;
          end;
         end;

procedure drawMarks(lineNumber: Integer);
       var
          i: integer;
     begin
          for i := 1 to 3 do
          begin
             write(' ');
             case (board[lineNumber, i]) of
                EMPTY:  write(' ');
                CROSS:  write('x');
                    ZERO: write('o');
         end;
         write(' ');
         if i < 3 then
         begin
            write(verticalbar);
             end;
      end;
          writeLn;
       end;

procedure drawLine;
       begin
      writeln(crossbar);
       end;

procedure drawBoard;
       begin
          drawMarks(1);
          drawLine;
          drawMarks(2);
          drawLine;
              drawMarks(3);
            end;



{------------------------}
Procedure StartGame;
var
Game,Comp: Boolean;
Row,Column,X,CompChoice,CompRow,CompColumn:Integer;
BEGIN
sndPlaySound('H:\Sound ForTTT\MP.wav',SND_ASYNC);
SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_BLUE or
                          FOREGROUND_INTENSITY);

Randomize;
writeln('During this game of tic-tac-toe you will be playing as the X.');
Game:=True;
While Game = true
do begin
Row:= 0;
Column:= 0;
X:= 0;
CompChoice:= 0;
CompRow:= 0;
CompColumn:= 0;
        Repeat
                X:= X+1;
                If X > 1
                then writeln('That spot is already taken');
                SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_INTENSITY);
writeln('    C1   C2    C3');
writeln('Row1         ');
writeln('Row2         ');
writeln('Row3         ');
SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_BLUE or
                          FOREGROUND_INTENSITY);
                Writeln('Please enter the row you wish to choose.(1 for row 1, 2 for row 2, etc)');
                readln(Row);
                writeln(' ');
                Writeln('Please enter the column you wish to choose.(1 for column 1, 2 for column 2, etc)');
                readln(Column);
                writeln(' ');
        Until Board[Row,Column] = EMPTY;
        sndPlaySound('H:\Sound ForTTT\Phaser.wav',SND_ASYNC);
        sndPlaySound('H:\Sound ForTTT\MP.wav',SND_ASYNC);
        X:=0;
        Board[Row,Column]:= CROSS;
        Repeat
                 Comp:= True;
                 CompChoice:= 1 + Random(3) ;
                 CompRow:= CompChoice;     ;
                 CompChoice:= 1 + Random(3)  ;
                 CompColumn:= CompChoice      ;
                 if (Board[1,1] <> Empty) and(Board[1,2] <> Empty) and(Board[1,3] <> Empty) and(Board[2,1] <> Empty) and(Board[2,2] <> Empty) and(Board[2,3] <> Empty) and(Board[3,1] <> Empty) and(Board[3,2] <> Empty) and(Board[3,3] <> Empty)
                 then begin
                        comp:= False;
                 end;
                 if Board[CompRow,CompChoice] = EMPTY
                 then Comp:= False;

        Until Comp =False;
        Board[CompRow,CompColumn]:= ZERO;
        SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_INTENSITY);

        DrawBoard;
        writeln(' ');
        SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_BLUE or
                          FOREGROUND_INTENSITY);


        {Wins across}
        {---------------------------}

        If (Board[1,1] = CROSS) and (Board[1,2] = CROSS) and (Board[1,3] = CROSS)
        then begin
                writeln('You have won!');
                sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;

        If (Board[2,1] = CROSS) and (Board[2,2] = CROSS) and (Board[2,3] = CROSS)
        then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;

        If (Board[3,1] = CROSS) and (Board[3,2] = CROSS) and (Board[3,3] = CROSS)
        then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;
        {---------------------------}

        {Wins downward}
        {---------------------------}
         If (Board[1,1] = CROSS) and (Board[2,1] = CROSS) and (Board[3,1] = CROSS)
        then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;

         If (Board[1,2] = CROSS) and (Board[2,2] = CROSS) and (Board[3,2] = CROSS)
        then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;

        If (Board[1,3] = CROSS) and (Board[2,3] = CROSS) and (Board[3,3] = CROSS)
        then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;
        {---------------------------}

        {Diagonal wins}
        {---------------------------}

        If (Board[1,1] = CROSS) and (Board[2,2] = CROSS) and (Board[3,3] = CROSS)
               then begin
                writeln('You have won!');
                 sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
                Game:= False;
        end;

        If (Board[1,3] = CROSS) and (Board[2,2] = CROSS) and (Board[3,1] = CROSS)
               then begin
               writeln('You have won!');
                sndPlaySound('H:\Sound ForTTT\appluase.wav',SND_ASYNC);
               Game:= False;
        end;
        {---------------------------}





        {Computer Wins}




        If Game = true
        then begin
        {Wins across}
        {---------------------------}

        If (Board[1,1] = ZERO) and (Board[1,2] = ZERO) and (Board[1,3] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;

        If (Board[2,1] = ZERO) and (Board[2,2] = ZERO) and (Board[2,3] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;

        If (Board[3,1] = ZERO) and (Board[3,2] = ZERO) and (Board[3,3] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;
        {---------------------------}

        {Wins downward}
        {---------------------------}
         If (Board[1,1] = ZERO) and (Board[2,1] = ZERO) and (Board[3,1] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;

         If (Board[1,2] = ZERO) and (Board[2,2] = ZERO) and (Board[3,2] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;

        If (Board[1,3] = ZERO) and (Board[2,3] = ZERO) and (Board[3,3] = ZERO)
        then begin
                writeln('You have lost!');
                Game:= False;
        end;
        {---------------------------}

        {Diagonal wins}
        {---------------------------}

        If (Board[1,1] = ZERO) and (Board[2,2] = ZERO) and (Board[3,3] = ZERO)
               then begin
                writeln('You have lost!');
                Game:= False;
        end;

        If (Board[1,3] = ZERO) and (Board[2,2] = ZERO) and (Board[3,1] = ZERO)
               then begin
               writeln('You have lost!');
               Game:= False;
        end;
        writeln(' ');
        {---------------------------}
        end;
        If (Board[1,1] <> EMPTY) and (Board[1,2] <> EMPTY) and (Board[1,3] <> EMPTY) and (Board[2,1] <> EMPTY) and (Board[2,2] <> EMPTY) and (Board[2,3] <> EMPTY) and (Board[3,1] <> EMPTY) and (Board[3,2] <> EMPTY) and (Board[3,3] <> EMPTY)
                then begin
                writeln('It was a tie');
                Game:= False
        end;

end;
END;

Procedure ShowImage(ImageName: string);
Var
 Bmp : VCL.Graphics.TBitmap;
begin
 Bmp:= VCL.Graphics.TBitmap.Create;
        try
        Bmp.LoadFromRescourceName(iHinstance, ImageName);
        writeln(format('%s is %d wide x %d high', [ImageName, Bmp.Width, Bmp.Height]));
        finally
        Bmp.Free;
        end;
 end;


begin
BRCC32 Pictures.RC
{Austin Schroeder}
ShowImage('Image1');
 SetConsoleTextAttribute(GetStdHandle(
                          STD_OUTPUT_HANDLE),
                          FOREGROUND_GREEN or
                          FOREGROUND_RED or
                          FOREGROUND_INTENSITY);
writeln(' _______     _______      ________ ');
writeln('/__ ___/    /_   __/     | _______]  ');
writeln(' / /       __/  /__      | |______ ');
writeln('/_/       /_______/      |________]');
sndPlaySound('H:\Sound ForTTT\gun_44mag_11',SND_ASYNC);
sleep(1000);
writeln('[------]');
writeln('[------]');
sleep(1000);
writeln(' ________       _          ________ ');
writeln('/___ ___/      / \        | _______]');
writeln('  / /         / _ \       | |       ');
writeln(' / /         / /_\ \      | |______');
writeln('/_/         /_/   \_\     |________]');
sndPlaySound('H:\Sound ForTTT\gun_44mag_11',SND_ASYNC);
sleep(1000);
writeln('[------]');
writeln('[------]');
sleep(1000);
writeln(' ________      _________        ________  ');
writeln('/___ ___/     / _____  /       |  ______]');
writeln('  / /        / /    / /        | |______  ');
writeln(' / /        / /____/ /         | |______  ');
writeln('/_/        /________/          |________] ');
sndPlaySound('H:\Sound ForTTT\gun_44mag_11',SND_ASYNC);
sleep(1000);
clrscrn;
sleep(1000);


write('W');
Sleep(70);
write('E');
Sleep(70);
write('L');
Sleep(70);
write('C');
Sleep(70);
write('M');
Sleep(70);
write('E');
Sleep(70);
write(' ');
Sleep(70);
write('T');
Sleep(70);
write('0');
Sleep(70);
write(' ');
Sleep(70);
write('T');
Sleep(70);
write('I');
Sleep(70);
write('C');
Sleep(70);
write('-');
Sleep(70);
write('T');
Sleep(70);
write('A');
Sleep(70);
write('C');
Sleep(70);
write('-');
Sleep(70);
write('T');
Sleep(70);
write('O');
Sleep(70);
write('E');
Sleep(70);
write('.');
Sleep(70);
Writeln(' ');
Sleep(70);
write('R');
Sleep(70);
write('E');
Sleep(70);
write('A');
Sleep(70);
write('D');
Sleep(70);
write('Y');
Sleep(70);
write(' ');
Sleep(70);
write('T');
Sleep(70);
write('O');
Sleep(70);
write(' ');
Sleep(70);
write('P');
Sleep(70);
write('L');
Sleep(70);
write('A');
Sleep(70);
write('Y');
Sleep(70);
write('?');
Sleep(70);
write('(');
Sleep(70);
write('y');
Sleep(70);
write('/');
Sleep(70);
write('n');
Sleep(70);
write(')');
Sleep(70);
WRITELN(' ');
readln(Again);
if Again = ('y')
then Play := True;

Play:= True;
While Play = True
do begin
 initBoard;
 StartGame;
 writeln('Would you like to play again(y/n)');
 readln(Again);
 If Again = ('n')
 then Play := False;
 ClrScrn;
end;
readln;
end.

At the moment I am only trying to insert a single picture, I am failing miserabley. This is all I have in my .rc
Image1 BITMAP Hello.BMP
It created the .res file, but it still is not working. I tried removing VCL, but it did absolutely nothing.
This is the list of files I have in the folder with the project.
Hello
pictures.~rc
pictures.rc
pictures.res
TICTACTOEVISUAL.~dpr
TICTACTOEVISUAL.cfg
TICTACTOEVISUAL.dof
TICTACTOEVISUAL
TICTACTOEVISUAL.exe

I apologize for the amount of work it has taken to help me, but I am still lost.

Hello is a .bmp

I don't think you are very lost. At least it isn't a huge journey from where you are to where you want to be. I haven't gone through your code line-by-line in detail but there doesn't seem to be a lot wrong with it in terms of syntax.

The first thing is that you have

BRCC32 Pictures.RC

in your source code. This is wrong. Delete it from there. You need to open a command prompt window (Start ... Accessories ... Command prompt). Then, in that command prompt window, change to the folder holding your project source code. Then type in

BRCC32 Pictures.RC

BRCC32 is a tool, a software program that compiles resource files such as your Pictures.rc file.

Next, you have a simple spelling error. LoadFromRescourceName should be LoadFromResourceName.

You will need to include Graphics in your uses clause. The way you do that should match the source code. So you could put

uses
  SysUtils,
  Windows,
  VCL.Graphics,
  MMSystem;

and then in your source you would have

Procedure ShowImage(ImageName: string);
Var
 Bmp : VCL.Graphics.TBitmap;
begin
  Bmp:= VCL.Graphics.TBitmap.Create;
  try
    Bmp.LoadFromResourceName  (Hinstance, ImageName);
    writeln(format('%s is %d wide x %d high', [ImageName, Bmp.Width, Bmp.Height]));
  finally
    Bmp.Free;
  end;
end;

This is the format for more recent Delphi versions. If you have an older version your uses clause would look like this:

uses
  SysUtils,
  Windows,
  Graphics,
  MMSystem;

and then in your source you would have

Procedure ShowImage(ImageName: string);
Var
 Bmp : Graphics.TBitmap;
begin
  Bmp:= Graphics.TBitmap.Create;
  try
    Bmp.LoadFromResourceName  (Hinstance, ImageName);
    writeln(format('%s is %d wide x %d high', [ImageName, Bmp.Width, Bmp.Height]));
  finally
    Bmp.Free;
  end;
end;

You may have to experiment a little to find the format that is needed for your Delphi version, but make sure the uses and the source use the same approach.

With those small changes I think your code should compile, and it should run. It won't do anything with the image yet, apart from report its size, but I guess that is your next step. The way you have it coded at the moment, the image is only available in the ShowImage routine. What do you ultimately want to do with the image?

I was wondering if I could display the image during the program run.

I also got everything else to work, I would like to thank you for your enormous help in my project. (I was using the older version)

I don't think you can really display an image in a console - it is intended for text, not graphics. At least if I wanted to display images in an app I would write a Windows app, even if I made it look like a console app.

It may be possible to fake it by creating a transparent window overlaying the console and displaying the image on that. Not sure about that but it is all I can think of.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.