Page 1 of 1

[Example] Converting all images to Jpegs

Posted: Sat Mar 25, 2006 12:29 pm
by Sergey Tkachenko
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 for TRichView 17.3 and newer, for all versions of C++Builder:

Code: Select all

#include "RVGrHandler.hpp"

void __fastcall TfrmMain::EnumItemsProc(TCustomRVData* RVData,
  int ItemNo, int& UserData1, const TRVUnicodeString UserData2,
  bool& ContinueEnum)
{
  ContinueEnum = true;
  if (RVData->GetItem(ItemNo)->InheritsFrom(__classid(TRVGraphicItemInfo)))
  {
	TRVUnicodeString s;
	TGraphic* gr;
	TRVVAlign VAlign;
	TRVTag Tag;
	RVData->GetPictureInfo(ItemNo,s,gr,VAlign,Tag);
	if (gr->InheritsFrom(__classid(TJPEGImage)))
	  return;
	Graphics::TBitmap* bmp = RVGraphicHandler->ToBitmap(gr);
	TJPEGImage* jpg = new TJPEGImage;
	jpg->Assign(bmp);
	delete bmp;
	RVData->SetPictureInfo(ItemNo,s,jpg,VAlign,Tag);
  }
}

void __fastcall TfrmMain::Button1Click(TObject *Sender)
{
  int v=0;
  RichViewEdit1->RVData->EnumItems(EnumItemsProc, v, "");
  RichViewEdit1->Invalidate();
}

The same code for TRichView 17.2 and older:

Code: Select all

void __fastcall TForm1::EnumItemsProc(TCustomRVData* RVData,
  int ItemNo, int& UserData1, const UnicodeString 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 6 and 20007, change the type of UserData2 from UnicodeString to AnsiSting.
Update:
2018-Apr-16: new code compatible with TRichView 17.3.

Posted: Sat Mar 25, 2006 12:30 pm
by Sergey Tkachenko
Similar procedure for Delphi, converting all images to bitmaps

Code for TRichView 17.3 and newer:

Code: Select all

uses RVTypes, RVGrHandler, CRVData;

procedure TForm1.EnumItemsProc(RVData: TCustomRVData;
  ItemNo: Integer; var UserData1: Integer; const UserData2: TRVUnicodeString;
  var ContinueEnum: Boolean);
var
  gr: TGraphic;
  bmp: TBitmap;
  Tag: TRVTag;
  VAlign: TRVVAlign;
  s: TRVUnicodeString;
begin
  if RVData.GetItem(ItemNo) is TRVGraphicItemInfo then
  begin
    RVData.GetPictureInfo(ItemNo,s,gr,VAlign,Tag);
    if not (gr is TBitmap) then
    begin
      bmp := RVGraphicHandler.ToBitmap(gr);
      RVData.SetPictureInfo(ItemNo,s,bmp,VAlign,Tag);
    end;
  end;
  ContinueEnum := True;
end;
The same code for TRichView 17.2 and older:

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; 
Update:
2018-Apr-16: new code compatible with TRichView 17.3.

Posted: Wed Apr 16, 2014 11:21 am
by Sergey Tkachenko
Converting all images to Jpegs, Delphi version.

Code for TRichView 17.3 and newer:

Code: Select all

uses CRVData, RVGrHandler, RVTypes;

procedure TForm1.EnumItemsProc(RVData: TCustomRVData;
  ItemNo: Integer; var UserData1: Integer; const UserData2: TRVUnicodeString;
  var ContinueEnum: Boolean);
var gr: TGraphic;
    jpg: TJPEGImage;
    bmp: TBitmap;
    Tag: TRVTag;
    VAlign: TRVVAlign;
    s: TRVUnicodeString;
begin
  if RVData.GetItem(ItemNo) is TRVGraphicItemInfo then
  begin
    RVData.GetPictureInfo(ItemNo,s,gr,VAlign,Tag);
    if not (gr is TJPEGImage) then
    begin
      bmp := RVGraphicHandler.ToBitmap(gr);
      jpg := TJPEGImage.Create;
      jpg.Assign(bmp);
      bmp.Free;
      RVData.SetPictureInfo(ItemNo,s, jpg,VAlign,Tag);
    end;
  end;
  ContinueEnum := True;
end;
The same code for TRichView 17.2 and older:

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; 
Update:
2018-Apr-16: new code compatible with TRichView 17.3