I am loading an image during a mail merge. I have registered a custom image format that I want loaded. So instead of using the Contents.LoadImageFromFile I am loading my own TPicture object and then assigning the graphic.
I am a little confused when assigning the object. I have three questions:
- Can I just assign my WPImage to Obj as in the code below or should I create a text object and then assign Obj := NewWPImage.ObjRef?
- Do I need to free the current contents of Obj?
- Do I need to set the height and width after setting the Obj?
The code I have below is working right now but I have started out but looking at how Contents.LoadImageFromFile was working. That code is confusing to me and has prompted these questions.
My Code:
MyPicture := TPicture.Create(); try MyPicture.LoadFromFile(AttachmentDir + DataValue); NewWPImg := TWPOImage.CreateImage(Contents.DataCollection, MyPicture.Graphic); PictureWidth := MyPicture.Width; PictureHeight := MyPicture.Height; finally FreeAndNil(MyPicture); end; // MaxWidth and MaxHeight are in Twips - I just need to scale them // to keep the picture in the right proportions WPScaleWH(PictureWidth, PictureHeight, MaxWidth, MaxHeight); NewWPImg.WidthTW := PictureWidth; NewWPImg.HeightTW := PictureHeight; Contents.Obj := NewWPImg;
From Contents.LoadImageFromFile. When I look at this code I wonder?
- Why was a TWPTextObj created instead of an WPOImage?
- Why was o.ObjRef assigned to Obj instead of o directly?
- When o is freed why is ObjRef still valid?
- What good does it do to set o.Width and o.Height and then immediately free o? Should that be setting
obj.Width and obj.Height? Is that needed at all since the ObjRef width and height is set?
o := TWPTextObj.Create;
Result := o.LoadObjFromFile(FileName, FDataCollection);
if Result then
begin
aobj := o.ObjRef;
Obj := aobj;
aobj.ScaleWH(abs(w), abs(h), true);
o.Width := aobj.WidthTW;
o.Height := aobj.HeightTW;
end
else o.Free;