Einzelnen Beitrag anzeigen

Geronimo

Registriert seit: 19. Dez 2006
Ort: Hamburg
38 Beiträge
 
#5

AW: Größe eines Bildes in JvRichEdit ändern

  Alt 20. Mär 2019, 21:31
Hallo Leute!

Falls es noch jemanden interessiert, ich hatte jetzt die Aufgabe, die Größe von Bildern in einem JvRichEdit zu ändern.
Mir fiel nichts besseres ein, als in den rtf-Code zu gehen.
Herausgekommen ist folgende Unit.
Möge die Community den Code verreißen!


Gruß
Geronimo



Code:
unit rtfPicResize;

interface

uses
  JvRichEdit,StrUtils,Classes,SysUtils;

type
  TrtfPic = record
    Width_Start: Integer;
    Width_End: Integer;
    Height_Start: Integer;
    Height_End: Integer;
  end;

type TrtfPics = array of TrtfPic;

function getRtfStringFromRichEdit(re: TjvRichEdit): String;
procedure WriteRtfStringtoRichEdit(rtfStr: String;var re: TjvRichEdit);
function CountPics(re: TjvRichEdit): TrtfPics;
function getPicHeight(PicIndex: Integer; re: TjvRichEdit): Integer;
procedure setPicHeight(PicIndex: Integer; AHeight: Integer;var re: TjvRichEdit);
function getPicWidth(PicIndex: Integer; re: TjvRichEdit): Integer;
procedure setPicWidth(PicIndex: Integer; AWidth: Integer;var re: TjvRichEdit);
function getLastPicIndex(re: TjvRichEdit): Integer;

implementation

function getRtfStringFromRichEdit(re: TjvRichEdit): String;
var
  ss: TStringStream;
begin
   try
     ss := TStringStream.Create;
     re.Lines.SaveToStream(ss);
     result := ss.DataString;
   finally
     ss.free;
   end;
end;

procedure WriteRtfStringtoRichEdit(rtfStr: String;var re: TjvRichEdit);
var
  ss: TStringStream;
begin
  try
    ss := TStringStream.Create(RtfStr);
    re.Lines.LoadFromStream(ss);
  finally
    ss.free;
  end;
end;

function CountPics(re: TjvRichEdit): TrtfPics;
var
  p1,p2: Integer;
  rtfStr: String;
begin
  p1 := 1;
  p2 := 1;
  setlength(Result,0);

  rtfStr := getRtfStringFromRichEdit(re);

  while p1 > 0 do begin
    p1 := posEx('picwgoal',rtfStr,p2);
    if p1 > 0 then begin
      p2 := posEx('\',rtfStr,p1);

      setlength(Result,length(Result)+1);
      Result[length(Result)-1].Width_Start := p1 +length('picwgoal');
      Result[length(Result)-1].Width_End := p2;

      p1 := posEx('pichgoal',rtfStr,p2);
      p2 := posEx(' ',rtfStr,p1);

      Result[length(Result)-1].Height_Start := p1 +length('pichgoal');
      Result[length(Result)-1].Height_End := p2;
    end;
  end;
end;

function getPicHeight(PicIndex: Integer; re: TjvRichEdit): Integer;
var
  rtfStr: String;
  Pics: TRtfPics;
  start,Ende: Integer;
begin
  rtfStr := getRtfStringFromRichEdit(re);
  Pics := CountPics(re);
  result := 0;

  if PicIndex <= length(Pics)-1 then begin
    Start := Pics[PicIndex].Height_Start;
    Ende := Pics[PicIndex].Height_End;
    result := strToInt(midStr(rtfStr,Start,Ende-Start));
  end;
end;

procedure setPicHeight(PicIndex: Integer; AHeight: Integer;var re: TjvRichEdit);
var
  rtfStr,NewRtfStr: String;
  Pics: TRtfPics;
  start,Ende: Integer;
  ss: TStringStream;
begin
  rtfStr := getRtfStringFromRichEdit(re);
  Pics := CountPics(re);

  if PicIndex <= length(Pics)-1 then begin
    Start := Pics[PicIndex].Height_Start;
    Ende := Pics[PicIndex].Height_End;
    NewRtfStr := LeftStr(rtfStr,Start-1)+IntToStr(AHeight)+RightStr(rtfStr,length(rtfStr)-ende+1);

    try
      ss := TStringStream.Create(NewRtfStr);
      re.Lines.LoadFromStream(ss);
    finally
      ss.Free;
    end;
  end;
end;

function getPicWidth(PicIndex: Integer; re: TjvRichEdit): Integer;
var
  rtfStr: String;
  Pics: TRtfPics;
  start,Ende: Integer;
begin
  rtfStr := getRtfStringFromRichEdit(re);
  Pics := CountPics(re);
  result := 0;

  if PicIndex <= length(Pics)-1 then begin
    Start := Pics[PicIndex].Width_Start;
    Ende := Pics[PicIndex].Width_End;
    result := strToInt(midStr(rtfStr,Start,Ende-Start));
  end;
end;

procedure setPicWidth(PicIndex: Integer; AWidth: Integer;var re: TjvRichEdit);
var
  rtfStr,NewRtfStr: String;
  Pics: TRtfPics;
  start,Ende: Integer;
  ss: TStringStream;
begin
  rtfStr := getRtfStringFromRichEdit(re);
  Pics := CountPics(re);

  if PicIndex <= length(Pics)-1 then begin
    Start := Pics[PicIndex].Width_Start;
    Ende := Pics[PicIndex].Width_End;
    NewRtfStr := LeftStr(rtfStr,Start-1)+IntToStr(AWidth)+RightStr(rtfStr,length(rtfStr)-ende+1);

    try
      ss := TStringStream.Create(NewRtfStr);
      re.Lines.LoadFromStream(ss);
    finally
      ss.Free;
    end;
  end;
end;

function getLastPicIndex(re: TjvRichEdit): Integer;
var
  Pics: TRtfPics;
begin
  Pics := CountPics(re);
  result := length(Pics)-1;
end;

end.
Die Welt ist rechteckig, linear und Gauss-verteilt!
  Mit Zitat antworten Zitat