- Posts: 13
- Thank you received: 0
×
CodeTyphon MS Windows (XP, Vista, Win7, Win8.x and Win10) OS Development, discussions and problems
Question Error Saving a JPegImage
- Robert Jenkins
- Topic Author
- Offline
- New Member
-
Less
More
9 months 2 weeks ago #16115
by Robert Jenkins
Error Saving a JPegImage was created by Robert Jenkins
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
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.
- Robert Jenkins
- Topic Author
- Offline
- New Member
-
Less
More
- Posts: 13
- Thank you received: 0
9 months 2 weeks ago - 9 months 2 weeks ago #16116
by Robert Jenkins
Replied by Robert Jenkins on topic Error Saving a JPegImage
Forgot to let you know
Typhon 7.5 x64 version
Windows 10 x64 with all updates
Robert
Typhon 7.5 x64 version
Windows 10 x64 with all updates
Robert
Last edit: 9 months 2 weeks ago by Robert Jenkins.
Please Log in or Create an account to join the conversation.
- Sternas Stefanos
-
- Away
- Moderator
-
- Ex Pilot, M.Sc, Ph.D
9 months 2 weeks ago - 9 months 2 weeks ago #16117
by Sternas Stefanos
PilotLogic Architect and Core Programmer
Replied by Sternas Stefanos on topic Error Saving a JPegImage
Thanks Sir
Please can you post the code of
ResizeJPeg procedure
In our test all are OK with out this procedure
zzz3.7z attach
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:
Last edit: 9 months 2 weeks ago by Sternas Stefanos.
Please Log in or Create an account to join the conversation.
- Robert Jenkins
- Topic Author
- Offline
- New Member
-
Less
More
- Posts: 13
- Thank you received: 0
9 months 2 weeks ago #16124
by Robert Jenkins
Replied by Robert Jenkins on topic Error Saving a JPegImage
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
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.
- Sternas Stefanos
-
- Away
- Moderator
-
- Ex Pilot, M.Sc, Ph.D
9 months 2 weeks ago - 9 months 2 weeks ago #16125
by Sternas Stefanos
PilotLogic Architect and Core Programmer
Replied by Sternas Stefanos on topic Error Saving a JPegImage
Sir
we test and with your ResizeJPeg procedure without problem
LAB CT 7.60 rev 007640 on Win10 Ent 64bit
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:
Last edit: 9 months 2 weeks ago by Sternas Stefanos.
Please Log in or Create an account to join the conversation.
- Robert Jenkins
- Topic Author
- Offline
- New Member
-
Less
More
- Posts: 13
- Thank you received: 0
9 months 2 weeks ago #16127
by Robert Jenkins
Replied by Robert Jenkins on topic Error Saving a JPegImage
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
Robert
Please Log in or Create an account to join the conversation.
- Sternas Stefanos
-
- Away
- Moderator
-
- Ex Pilot, M.Sc, Ph.D
9 months 2 weeks ago - 9 months 2 weeks ago #16129
by Sternas Stefanos
PilotLogic Architect and Core Programmer
Replied by Sternas Stefanos on topic Error Saving a JPegImage
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
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
Last edit: 9 months 2 weeks ago by Sternas Stefanos.
The following user(s) said Thank You: Robert Jenkins
Please Log in or Create an account to join the conversation.
- Robert Jenkins
- Topic Author
- Offline
- New Member
-
Less
More
- Posts: 13
- Thank you received: 0
9 months 2 weeks ago - 9 months 2 weeks ago #16131
by Robert Jenkins
Replied by Robert Jenkins on topic Error Saving a JPegImage
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
Thank you VERY much for you help on this issue and EVERYTHING you do with CodeTyhpon!!! CodeTyphon is my favorite development tool!!
Robert
Last edit: 9 months 2 weeks ago by Robert Jenkins.
Please Log in or Create an account to join the conversation.
- Sternas Stefanos
-
- Away
- Moderator
-
- Ex Pilot, M.Sc, Ph.D
9 months 2 weeks ago #16135
by Sternas Stefanos
PilotLogic Architect and Core Programmer
Replied by Sternas Stefanos on topic Error Saving a JPegImage
Thanks Sir
please check and new bs_EnginesPlus pkg
has some functions for you.
Have fun
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.