[Example] Converting all images to Jpegs

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 13150
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] Converting all images to Jpegs

Post by Sergey Tkachenko » Sat Mar 25, 2006 12:29 pm

This C++ code converts all images in RichViewEdit1 to Jpeg format.
This is not an editing procedure (it cannot be undone and redone by users).

This code uses RVData->EnumItems() which calls the specified procedure (EnumItemsProc in this demo) for all items.

Code: Select all

void __fastcall TForm1::EnumItemsProc(TCustomRVData* RVData,
  int ItemNo, int& UserData1, const AnsiString UserData2,
  bool& ContinueEnum)
{
  ContinueEnum = true;
  if (RVData->GetItem(ItemNo)->InheritsFrom(__classid(TRVGraphicItemInfo)))
  {
    AnsiString s;
    TGraphic* gr;
    TRVVAlign VAlign;
    TRVTag Tag;
    RVData->GetPictureInfo(ItemNo,s,gr,VAlign,Tag);
    if (gr->InheritsFrom(__classid(TJPEGImage)))
      return;
    Graphics::TBitmap* bmp = new Graphics::TBitmap;
    try
    {
      bmp->Assign(gr);
    }
    catch(...)
    {
      bmp->Width = gr->Width;
      bmp->Height = gr->Height;
      bmp->Canvas->Draw(0, 0, gr);
    }
    TJPEGImage* jpg = new TJPEGImage;
    jpg->Assign(bmp);
    delete bmp;
    RVData->SetPictureInfo(ItemNo,s,jpg,VAlign,Tag);
  }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int v=0;
  RichViewEdit1->RVData->EnumItems(EnumItemsProc, v, "");
  RichViewEdit1->Invalidate();
}
For C++Builder 2009+, change the type of UserData2 from AnsiSting to UnicodeString.
Last edited by Sergey Tkachenko on Wed Apr 16, 2014 11:20 am, edited 2 times in total.

Sergey Tkachenko
Site Admin
Posts: 13150
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Sat Mar 25, 2006 12:30 pm

Similar procedure for Delphi, converting all images to bitmaps

Code: Select all

procedure TForm1.EnumItemsProc(RVData: TCustomRVData; 
  ItemNo: Integer; var UserData1: Integer; const UserData2: String; 
  var ContinueEnum: Boolean); 
var gr: TGraphic; 
    bmp: TBitmap; 
    Tag: TRVTag; 
    VAlign: TRVVAlign; 
    s: TRVAnsiString; 
begin 
  if RVData.GetItem(ItemNo) is TRVGraphicItemInfo then begin 
    RVData.GetPictureInfo(ItemNo,s,gr,VAlign,Tag); 
    bmp := TBitmap.Create; 
    try 
      bmp.Assign(gr); 
    except 
      bmp.Width := gr.Width; 
      bmp.Height := gr.Height; 
      bmp.Canvas.Draw(0, 0, gr); 
    end; 
    RVData.SetPictureInfo(ItemNo,s,bmp,VAlign,Tag); 
  end; 
  ContinueEnum := True; 
end; 
Call:

Code: Select all

var v: Integer; 
begin 
  v := 0; 
  RichViewEdit1.RVData.EnumItems(EnumItemsProc, v, '') 
end; 

Sergey Tkachenko
Site Admin
Posts: 13150
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Wed Apr 16, 2014 11:21 am

Converting all images to Jpegs, Delphi version.

Code: Select all

procedure TForm3.EnumItemsProc(RVData: TCustomRVData;
  ItemNo: Integer; var UserData1: Integer; const UserData2: String;
  var ContinueEnum: Boolean);
var gr: TGraphic;
    jpg: TJPEGImage;
    bmp: TBitmap;
    Tag: TRVTag;
    VAlign: TRVVAlign;
    s: TRVAnsiString;
begin
  if RVData.GetItem(ItemNo) is TRVGraphicItemInfo then begin
    RVData.GetPictureInfo(ItemNo,s,gr,VAlign,Tag);
    if gr is TJPEGImage then
      exit;
    bmp := TBitmap.Create;
    try
      bmp.Assign(gr);
    except
      bmp.Width := gr.Width;
      bmp.Height := gr.Height;
      bmp.Canvas.Draw(0, 0, gr);
    end;
    jpg := TJPEGImage.Create;
    jpg.Assign(bmp);
    bmp.Free;
    RVData.SetPictureInfo(ItemNo,s, jpg,VAlign,Tag);
  end;
  ContinueEnum := True;
end;
Call:

Code: Select all

var v: Integer; 
begin 
  v := 0; 
  RichViewEdit1.RVData.EnumItems(EnumItemsProc, v, '') 
end; 

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest