Welcome, Guest
Username: Password: Remember me
CodeTyphon MS Windows (Win7, Win8.x, Win10 and Win11) OS Development, discussions and problems
  • Page:
  • 1

TOPIC:

Error Saving a JPegImage 2 years 6 months ago #16115

  • Robert Jenkins
  • Robert Jenkins's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 15
  • Thank you received: 0
Once I modify a JPEG image I can't save it to a stream or file.  Always gets "File not open." error message.  No errors with PNG or BITMAP images. 

basic code example:

Procedure Tform1.ButtonLoadImageClick(Sender : Tobject);
Var
  AFileName : TFileName;
  AJPeg : TJPegImage;
  AStream : TMemoryStream;
Begin
  If GetFileName(AFileName, 'JPEG Images (*.jpg)|*.jpg') Then  //  Just a dialog box to select a jpeg image
  Begin
    AJPeg := TJPegImage.Create;
    Try
      AJPeg.LoadFromFile(AFileName);
      ResizeJPeg(AJpeg, 200, 200);
      Image1.Picture.Assign(AJPeg);  //  resized image displays fine here
      AStream := TMemoryStream.Create;
      Try
        AJPeg.SaveToStream(AStream);  //  Here is the error "File not open."
      Finally
        FreeAndNil(AStream);
      End;
    Finally
      FreeAndNil(AJPeg);
    End;
  End;
End;     

Thanks

Robert

Please Log in or Create an account to join the conversation.

Error Saving a JPegImage 2 years 6 months ago #16116

  • Robert Jenkins
  • Robert Jenkins's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 15
  • Thank you received: 0
Forgot to let you know

Typhon 7.5 x64 version
Windows 10 x64 with all updates

Robert

Please Log in or Create an account to join the conversation.

Last edit: by Robert Jenkins.

Error Saving a JPegImage 2 years 6 months ago #16117

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4506
  • Thank you received: 1100
Thanks Sir

Please can you post the code of
ResizeJPeg procedure

In our test all are OK with out this procedure

zzz3.7z attach
PilotLogic Architect and Core Programmer
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Error Saving a JPegImage 2 years 6 months ago #16124

  • Robert Jenkins
  • Robert Jenkins's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 15
  • Thank you received: 0
If I do not modify the JPEG it seems to be fine.  If I just load and save all works.  But when loading a PNG and converting to a JPEG it gives the error.  And when resizing a JPEG it gives that error.  No errors doing same things with PNG.  I can load JPEG and convert to PNG just fine.  I can resize a PNG just fine.  It is only when modifying a JPEG and then trying to save it.  It appears to be an issue in the SaveToStream procedure but I tried to trace through with the debugger and could not figure it out. 

Here is the code I have been using to resize a JPEG.  If you just change the data type to a PNG it works for a PNG fine.


Procedure ResizeJPeg(Const AJPegImage: TJPegImage; Const AWidth, AHeight: Integer);
Var
  ARect:    TRect;
  ATempJPeg: TJPegImage;
  X, Y:     Integer;
Begin
  If (AJPegImage.Width = AWidth) And (AJPegImage.Height = AHeight) Then Exit;

  FillChar(ARect, SizeOf(TRect), 0);
  If (AJPegImage.Width > AJPegImage.Height) Then
  Begin
    ARect.Right := AWidth;
    ARect.Bottom := ((AWidth * AJPegImage.Height) Div AJPegImage.Width);
    X := 0;
    Y := (AHeight Div 2) - (ARect.Bottom Div 2);
  End
  Else
  Begin
    ARect.Right := ((AHeight * AJPegImage.Width) Div AJPegImage.Height);
    ARect.Bottom := AHeight;
    X := (AWidth Div 2) - (ARect.Right Div 2);
    Y := 0;
  End;

  ATempJPeg := TJPegImage.Create;
  Try
    ATempJPeg.PixelFormat := AJPegImage.PixelFormat;
    ATempJPeg.Transparent := AJPegImage.Transparent;
    ATempJPeg.TransparentMode := AJPegImage.TransparentMode;
    ATempJPeg.TransparentColor := AJPegImage.TransparentColor;
    ATempJPeg.SetSize(AWidth, AHeight);
    ATempJPeg.Canvas.FillRect(ATempJPeg.Canvas.ClipRect);
    ATempJPeg.Canvas.StretchDraw(ARect, AJPegImage);

    AJPegImage.SetSize(AWidth, AHeight);
    AJPegImage.Canvas.FillRect(AJPegImage.Canvas.ClipRect);
    AJPegImage.Canvas.Draw(X, Y, ATempJPeg);
  Finally
    FreeAndNil(ATempJPeg);
  End;
End;

If you suggest a better routine to resize a JPEG that would be fine as well.  Speed is not so important for what I am doing.

Here is my routine for loading a PNG and converting it to a JPEG.  The routine seems to work but when I attempt to save the resulting JPEG I get the same error.  It does display correctly in a TImage.

Function PngToJPeg(Const APng: TPngImage; Const AJPeg: TJPegImage): Boolean;
Begin
  Result := False;
  Try
    AJPeg.Clear;
    AJPeg.Transparent := APng.Transparent;
    If (AJPeg.Transparent) Then
    Begin
      AJPeg.TransparentColor := APng.TransparentColor;
      AJPeg.TransparentMode := APng.TransparentMode;
    End;
    AJPeg.Width := APng.Width;
    AJPeg.Height := APng.Height;
    AJPeg.PixelFormat := APng.PixelFormat;
    AJPeg.Canvas.Draw(0, 0, APng);
    Result := True;
  Except
    Result := False;
  End;
End;
 

Robert

Please Log in or Create an account to join the conversation.

Error Saving a JPegImage 2 years 6 months ago #16125

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4506
  • Thank you received: 1100
Sir
we test and with your ResizeJPeg procedure without problem
LAB CT 7.60 rev 007640 on Win10 Ent 64bit

 

 
PilotLogic Architect and Core Programmer
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Error Saving a JPegImage 2 years 6 months ago #16127

  • Robert Jenkins
  • Robert Jenkins's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 15
  • Thank you received: 0
Thanks for looking into this.  I will either reinstall current release or upgrade to latest development version and try again.  I did add a patch to fix an issue with QR code generation but outside of that I have not modified any core code. 

Robert

Please Log in or Create an account to join the conversation.

Error Saving a JPegImage 2 years 6 months ago #16129

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4506
  • Thank you received: 1100
Patch to fix QR code generation NOT needed to LAB CT rev 007540.

CodeTyphon never need reinstall.
To rebuild CT just CTCenter=>CodeTyphon=> 'Remove and build ALL".
will remove ALL and Build ALL
 
PilotLogic Architect and Core Programmer
The following user(s) said Thank You: Robert Jenkins

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Error Saving a JPegImage 2 years 6 months ago #16131

  • Robert Jenkins
  • Robert Jenkins's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 15
  • Thank you received: 0
I used CT Center to download "LAB Development Version (Experimental)".  After everything downloaded and compiled there are no errors with JPEG.  I also double checked my QR Code generation tool and it also compiled and ran fine with no error. 

Thank you VERY much for you help on this issue and EVERYTHING you do with CodeTyhpon!!!  CodeTyphon is my favorite development tool!!

Robert

Please Log in or Create an account to join the conversation.

Last edit: by Robert Jenkins.

Error Saving a JPegImage 2 years 6 months ago #16135

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4506
  • Thank you received: 1100
Thanks Sir
please check and new bs_EnginesPlus pkg
has some functions for you.

Have fun
PilotLogic Architect and Core Programmer
The following user(s) said Thank You: Robert Jenkins

Please Log in or Create an account to join the conversation.

  • Page:
  • 1