Page 1 of 1

Image Copy from one sheet to new sheet

Posted: Thu Nov 05, 2015 10:54 am
by huseyinxy
Hello,

I want to copy images from one sheet to antoher sheet, there are two problems:

1. Sheet.Drawing.InsertImage method not worked with stream)
2. Image Scale. I used InsertImage with filename, this worked but previous image scale not preserved although I set new size.

Component version 5.20.61

test code like this

procedure TForm1.Button1Click(Sender: TObject);

const
fnXls = 'D:\Tests\xlsImages\dsi1.xls';
fnXls2 = 'D:\Tests\xlsImages\dsi2.xls';

var
Excel: TXlsReadWriteII5;
nSheet: TXLSWorkSheet;

i: integer;
fn: string;
nImage, Image: TXLSDrawingImage;
ST: TStream;
Editor: TXLSDrawingEditorImage;
cmW, cmH: double;
begin
Excel := TXLSReadWriteII5.Create(nil);
Excel.Filename := fnXls;
Excel.Read;

// add new sheet
nSheet := Excel.Add;
Excel.CopySheet( 0, nSheet.Index );

// copy images
for i:= 0 to Excel[0].Drawing.Images.Count-1 do begin
Image := Excel[0].Drawing.Images;


// save image height and width
Editor := Excel[0].Drawing.EditImage(Image);
cmH := Editor.CmHeight;
cmW := Editor.CmWidth;
Editor.Free;


// ?? not worked
// ST := TMemoryStream.Create;
// Image.SaveToStream(st);
// nImage := nSheet.Drawing.InsertImage(Image.UniqueName, ST, image.Col1, image.Row1, image.Col1Offs, Image.Row1Offs);
// ST.Free;

// save image to a file
fn := ChangeFileExt(fnXls, '.jpg');
Image.SaveToFile(fn);

// load saved images to new sheet
nImage := nSheet.Drawing.InsertImage(fn, image.Col1, image.Row1, image.Col1Offs, Image.Row1Offs, 1);

// set new image size
Editor := nSheet.Drawing.EditImage(nImage);
Editor.CmHeight := cmH;
Editor.CmWidth := cmW;
Editor.Free;
end;

Excel.SaveToFile(fnXls2);
Excel.Free;
end;

Thanks.