Recent

Author Topic: Store an image inside the app  (Read 11387 times)

zoiddani

  • New Member
  • *
  • Posts: 29
Store an image inside the app
« on: January 23, 2011, 02:54:27 pm »
Hello,
I've been writing a minesweeper game, I got pretty far, but I encountered the following problem:
I'm duplicating a little (20x20) picture on my canvas. I store that icon in an Image. However, I can't duplicate it right. My icon has to be visible, if i want to copy it. Could I store it somewhere where i can't see it, but I could get acces to its pixels?

thank you

jw

  • Full Member
  • ***
  • Posts: 126
Re: Store an image inside the app
« Reply #1 on: January 23, 2011, 09:18:15 pm »
usually I'm the one doing the asking but ok I'll give it a shot.

store the 20x20 picture in a timagelist

I don't know why it would have to be visible to copy? where's an example of code you use to copy or draw the image over and over again now?

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Store an image inside the app
« Reply #2 on: January 24, 2011, 07:18:18 pm »
well i use this simple code:

Code: [Select]
for i:= 0 to 19 do begin;
  for j:= 0 to 19 do begin;
     Image1.Canvas.Pixels[somenum+i,anothernum+i]:=Image2.Canvas.Pixels[i,j];
  end
end

Where Image1 is the main canvas i draw on, and Image2 contains the icon. I know the problem is that I'm using Pixels, but I can't find another way to copy it.

I will try your TImageList now, and see how far i can get with it :)

Edit: I realised that the program might not be with be with the container but with the code.
I have to avoid the Canvas to copy the picture. Already tried Copyrect, didn't work, because it uses the canvas.
 
However, jw, thanks for the tip :)
« Last Edit: January 24, 2011, 08:11:58 pm by zoiddani »

mas steindorff

  • Hero Member
  • *****
  • Posts: 587
Re: Store an image inside the app
« Reply #3 on: January 24, 2011, 08:24:38 pm »
I just drop a Timage per picture and then with the Picture properties access by the object inspector, load it with the image from file (there is a lot it supports).
I loaded 3 or 4 images that way and then set my visible play field images to the image of choice at run-time
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Store an image inside the app
« Reply #4 on: January 25, 2011, 04:23:31 pm »
I don't really understand your last sentence.
Basically, i need something what i can copy many times to different positions

mas steindorff

  • Hero Member
  • *****
  • Posts: 587
Re: Store an image inside the app
« Reply #5 on: January 25, 2011, 07:04:07 pm »
I loaded 3 or 4 images that way and then set my visible play field images to the image of choice at run-time
how about the code for my tic tac toe game itself.  I have 9 blank Timages, all with the same onClick call.  it checks to see if the square is used with the 1st fun() and then selects the square with an X or O image that have been preloaded with their images.  I just copy the image to the blank image by calling one of the other fun() shown.

I believe this is the same thing you wish to do.

Code: [Select]
function TFMain.SquareUsed( Sender: TObject): boolean;
var pic:TImage;
begin
  pic := TImage(sender);
  result := (pic.tag <> 0);
end;

procedure TFMain.PlaceX(Sender:TObject);
var    pic:TImage;
begin
  pic := TImage(sender);
  pic.Picture:= imgX.Picture;
  pic.Tag:=2;
end;

procedure TFMain.PlaceO(Sender:TObject);
var    pic:TImage;
begin
  pic := TImage(sender);
  pic.Picture:= imgO.Picture;
  pic.Tag:=1;
end;

Procedure TFMain.PlaceBlank(Sender:TObject);
var    pic:TPanSquare;
begin
  pic := TImage(sender);
  pic.Picture:= nil;
  pic.Tag:=0;
end;         
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Store an image inside the app
« Reply #6 on: January 25, 2011, 08:23:03 pm »
I understand your code, but my problem is that I use one big canvas, not little TImages. I need a code which copies a picture on my canvas.
I saw that my image has a property named newinstance. Maybe that could help me out, that i make instances of it. Is there a possibility to do this? How could I create a new instance. A tried just putting in the newinstance code, however nothing happened. Sorry, but I'm pretty new to OOP. Anyway, thanks maas steindorff!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Store an image inside the app
« Reply #7 on: January 26, 2011, 11:59:00 am »
TCanvas has also Draw() function you can use instead of the Pixels. You can draw a canvas to another canvas with it.

mas steindorff

  • Hero Member
  • *****
  • Posts: 587
Re: Store an image inside the app
« Reply #8 on: January 26, 2011, 07:16:54 pm »
I understand your code, but my problem is that I use one big canvas, not little TImages. I need a code which copies a picture on my canvas.
You will need to directly modify the image each time.  based on the response you have gotten, I don't think there is an easy way to do that without a pixel by pixel operation.

I'm pretty new to OOP. ...
the OOP way is to divide or and conquer.   as you see by my code, each image ends up calling the same code.  All of my images used the same "on click" event and simply pass "self" as the parameter to the posted code.

if not SquareUsed(sender) then PlaceX(sender)  is the basic code once I figure out X or O's turn
If I were to do the mine sweeper game, I would create the timages at run time so the grid can be changed by the user.
I use the TImage's tag to store the status of each square.  You can do the same thing to store your bomb count and if it has been clicked yet of not.
when you create all of your timages, you can set them as an array or you can add them to an array of pointer to tmiages so your code can scan the list quickly.  just remember to set the parent link so you well get the hooks you need for the click and redraw to be handled automatically
« Last Edit: January 26, 2011, 08:47:18 pm by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Store an image inside the app
« Reply #9 on: January 27, 2011, 03:51:58 am »
I've been writing a minesweeper game, I got pretty far, but I encountered the following problem:
I'm duplicating a little (20x20) picture on my canvas. I store that icon in an Image. However, I can't duplicate it right. My icon has to be visible, if i want to copy it. Could I store it somewhere where i can't see it, but I could get acces to its pixels?
Just happened to read first post morecarefully, so i'll present CopyRect() function...

Code: [Select]
var tempBmp: TBitmap;
...
(formCreate)
tempBmp:=TBitmap.Create;
tempBmp.Width:=20;
tempBmp.Height:=20;
...
(draw to tempBmp)
tempBmp.canvas.CopyRect(bounds(0,0,20,20),image1.canvas,bounds(gx,gy,20,20));

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Store an image inside the app
« Reply #10 on: January 29, 2011, 09:24:05 am »
Thank you for your help.
The draw function solved it.
I simply used Draw(x,y,Image2.Picture.Graphic). So I could access it's picture and now it works. Thank you all!

 

TinyPortal © 2005-2018