본문 바로가기
Application/Delphi Lecture

rxrichedit 이미지나 문자 넣기.

by 현이빈이 2008. 8. 13.
반응형
function ConvertBitmapToRTF(const Bitmap: TBitmap): string;
var
 bi, bb: string;
 bis, bbs: Cardinal;
 achar: string[2];
 Buffer: string;
 I: Integer;
type
 PWord = ^Word;
begin
 GetDIBSizes(Bitmap.Handle, bis, bbs);
 SetLength(bi, bis);
 SetLength(bb, bbs);
 GetDIB(Bitmap.Handle, Bitmap.Palette, PChar(bi)^, PChar(bb)^);
 SetLength(Buffer, (Length(bb) + Length(bi)) * 2);
 i := 1;
 for bis := 1 to Length(bi) do
 begin
   achar := IntToHex(Integer(bi[bis]), 2);
   PWord(@Buffer[i])^ := PWord(@achar[1])^;
   inc(i, 2);
 end;
 for bbs := 1 to Length(bb) do
 begin
   achar := IntToHex(Integer(bb[bbs]), 2);
   PWord(@Buffer[i])^ := PWord(@achar[1])^;
   inc(i, 2);
 end;
 Result := '{\rtf1 {\pict\dibitmap ' + Buffer + ' }}';
end;

procedure InsertBitmapIntoRxRichEdit(const Bitmap: TBitmap; const RxRichEdit:
 TRxRichEdit); overload;
begin
 RxRichEdit.SelText := ConvertBitmapToRTF(Bitmap);
 RxRichEdit.SelLength := 0;
 RxRichEdit.SelStart := RxRichEdit.SelStart + 1;
end;

procedure InsertBitmapIntoRxRichEdit(const GraphicFileName: string; const
 RxRichEdit: TRxRichEdit); overload;
var
 Bitmap: TBitmap;

 Graphic: TPicture;
begin
 Graphic := TPicture.Create;
 try
   Graphic.LoadFromFile(GraphicFileName);

   if Graphic.Graphic is TBitmap then
     Bitmap := Graphic.Bitmap
   else
   begin
     Bitmap := TBitmap.Create;
     Bitmap.Assign(Graphic.Graphic);
   end;

   InsertBitmapIntoRxRichEdit(Bitmap, RxRichEdit);
 finally
   if Bitmap <> Graphic.Bitmap then
     FreeAndNil(Bitmap);

   FreeAndNil(Graphic);
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 //InsertBitmapIntoRxRichEdit('C:\Temp\Untitled.bmp', RxRichEdit1);
 InsertBitmapIntoRxRichEdit('C:\Temp\Untitled-8.jpg', RxRichEdit1);
 RxRichEdit1.SetFocus;
end;
end.
반응형