Hi xinyiman,
Unfortunately it seems like i am a bit lost with my English skills, as i did not completely understood what you asked for exactly
(so please accept my apologies, in case i answered wrongly).
To do that you change the loading LoadFromFile how? Does this automatically?
Yes, FPimage works a bit differently from what we are used to with Lazarus.
AFAIK you need to define the dimensions yourself and this can't be done automatically (please, someone correct me when wrong).
Then I would understand, I can not put the contents of the image in the canvas, I change the canvas, but if I want to reset the image to the canvas changed and then save it as I do?
If i understood correctly (also by having looked at your code), then you seem to be unable to save your copied rectangle to a new file on disk ?
That is probably because you are using two canvases on the same image...
CanvasOrig := TFPImageCanvas.Create (image);
CanvasDest := TFPImageCanvas.Create (image);
I have modified your code a little, and the following seems to work for me:
program test1;
{$MODE OBJFPC}{$H+}
Uses
FPImage, FPCanvas, FPImgCanv,
FPReadPNG, FPWritePNG;
procedure CopyPartOfImage(Origine, Destinazione: TFPCustomCanvas; Top, Left, Height, Width: integer);
var
x,y : integer;
i,j : integer;
begin
i := 0;
//Destinazione.clear;
for y := Top to (Top + Height) do
begin
j:=0;
for x := Left to (Left + Width) do
begin
Destinazione.Colors[j, i] := Origine.Colors[x, y];
inc(j);
end;
inc(i);
end;
end;
procedure ElaboraSingolaImmagine(AFileName, ADestFileName: string);
var
SourceImage : TFPCustomImage;
SourceCanvas : TFPCustomCanvas;
DestinationImage : TFPCustomImage;
DestinationCanvas : TFPCustomCanvas;
reader : TFPCustomImageReader;
writer : TFPCustomImageWriter;
begin
// Create in memory image
SourceImage := TFPMemoryImage.Create(144, 144);
// Create appropiate reader
Reader := TFPReaderPNG.Create;
// Read imagedata from file
SourceImage.LoadFromFile(AFileName, Reader);
// Create canvas for the image
SourceCanvas := TFPImageCanvas.Create(SourceImage);
// Prepare destination
DestinationImage := TFPMemoryImage.Create(144, 144);
DestinationCanvas := TFPImageCanvas.Create(DestinationImage);
CopyPartOfImage(SourceCanvas, DestinationCanvas, 0, 0, 50, 50);
Writer := TFPWriterPNG.Create;
DestinationImage.SaveToFile(ADestFileName, Writer);
// Clean up
SourceImage.Free;
DestinationImage.Free;
SourceCanvas.Free;
DestinationCanvas.Free;
Reader.Free;
Writer.Free;
end;
//
// GreenSwirl.png (144 x 144)
//
begin
ElaboraSingolaImmagine('greenswirl.png', 'result1.png');
end.
It might perhaps not be entirely what you are looking for, but hopefully can aid you with your questions.
BTW: i noticed some other postings from you concerning images.
In case these questions all belong to the same project then it would perhaps be an idea to do some research on what would be the best option (read: graphics package or other helpful related functionality) that are better suited for helping you out with your project.
edit: a couple of "perhaps" too many -> rephrased and removed