[Example] Converting all images to Jpegs
Posted: 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 for TRichView 17.3 and newer, for all versions of C++Builder:
The same code for TRichView 17.2 and older:
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.
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();
}
Update:
2018-Apr-16: new code compatible with TRichView 17.3.