Lex34
Lex34

Reputation: 21

Why does transfer of BitMap reverse order in Delphi?

When the button is clicked, images should be transferred from richedit1 to richedit2 and displayed in order, and they are displayed in reverse. How to fix it? Below is the code.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
    InsertBitmapToRE(RichEdit1.Handle, Image1.Picture.Bitmap.Handle);
  end;
end;

Function MAP_LOGHIM_TO_PIX(Const Val: Integer; Const Log: Integer): Integer;
Const
  HIMETRIC_PER_INCH=2540;
Begin
  Result:=MulDiv(Val, Log, HIMETRIC_PER_INCH);
End;

Function MAP_LOGHIMPT_TO_PT(Const Val: TPoint; Const Handle: HWND = 0): TPoint;
Var
  DC: HDC;
Begin
  DC:=GetDC(Handle);
  Result.X:=MAP_LOGHIM_TO_PIX(Val.X, GetDeviceCaps(DC, LOGPIXELSX));
  Result.Y:=MAP_LOGHIM_TO_PIX(Val.Y, GetDeviceCaps(DC, LOGPIXELSY));
  ReleaseDC(Handle, DC);
End;

procedure TForm1.Button2Click(Sender: TObject);
Var
  IREO: IRichEditOle;
  OleClientSite: IOleClientSite;
  ReObject: TReObject;
  I: Integer;
  ViewObject2: IViewObject2;
  Rc: TRect;
  Path:String;
  bmp:TBitmap;
  Pt: TPoint;
begin
  Path:='C:\temp\richedit\';
  SendMessage(RichEdit1.Handle, EM_GETOLEINTERFACE, 0, Longint(@IREO));
  IREO.GetClientSite(OleClientSite);
  For I:=IREO.GetObjectCount-1 Downto 0 Do
  Begin
    ZeroMemory(@ReObject, SizeOf(ReObject));
    ReObject.cbStruct:=SizeOf(ReObject);
    If Succeeded(IREO.GetObject(I, ReObject, $00000001)) Then
    If Succeeded(ReObject.poleobj.QueryInterface(IViewObject2, ViewObject2)) Then
    Begin
      ViewObject2.GetExtent(DVASPECT_CONTENT, -1, Nil, Pt);
      Pt:=MAP_LOGHIMPT_TO_PT(Pt, RichEdit1.Handle);
      bmp:=TBitmap.Create;
      Bmp.Height := Pt.Y;
      Bmp.Width := Pt.X;
      SetRect(Rc, 0, 0, Bmp.Width, Bmp.Height);
      OleDraw(ReObject.poleobj, DVASPECT_CONTENT, bmp.Canvas.Handle, Rc);
      bmp.SaveToFile(Path+'Img'+IntToStr(I+1)+'.bmp');
      InsertBitmapToRE(RichEdit2.Handle, bmp.Handle);
    End
    Else
      ShowMessage('Error: Can''t get IViewObject2');
  End;
end;

screenshot

Screenshot

Upvotes: 2

Views: 179

Answers (1)

Brian
Brian

Reputation: 7299

Using downto 0 in a for loop is efficient but counts in reverse so is not always desirable. This is such a case since you are using the loop variable as an index and want a specific order of processing.

For I:= 0 to IREO.GetObjectCount-1 Do

Upvotes: 4

Related Questions