CompressImageFileToStream
Function CompressImageFileToStream(
const aOriginalImageFile: string;
out aNewImageStream: TStream;
const aQuality: integer): boolean
Example
procedure ScriptEvent(var Value: variant);
var
Output: TMemoryStream;
Succeeded: Boolean;
begin
Output := TMemoryStream.Create;
try
// The out parameter is misleading: Output must already exist and be writable.
Succeeded := CompressImageFileToStream(
'C:\VeloxData\Examples\source.jpg', Output, 75);
if Succeeded then
Output.Position := 0;
Value := Succeeded;
finally
Output.Free;
end;
end;
Usage
CompressImageFileToStream re-encodes a JPEG or PNG source file into a caller-owned writable stream without resizing it.
Parameters
| Name | Type | Description |
|---|---|---|
aOriginalImageFile | string, const | Existing .jpg, .jpeg or .png source selected by case-insensitive extension. |
aNewImageStream | TStream, out | Misdeclared destination. It must already refer to an open writable stream; the function never assigns or frees it. |
aQuality | integer, const | Direct JPEG quality or defective mapped PNG compression level. |
Returns
False when the source file is absent or the destination stream reference is nil. True only after the selected VCL graphic completes SaveToStream. Other failures normally raise.
Errors
Codec, invalid quality, permission, read, memory and destination-write errors are raised to the script. An unsupported extension can cause an object-reference error. The function does not roll back bytes already written to the destination.
Usage notes
For a reusable memory stream, set Position := 0 and Size := 0 before the call. Validate extension and quality first. Do not interpret out as allocation or ownership transfer.
Additional Technical Info
CompressImageFileToStream loads a JPEG or PNG chosen by source filename extension, re-encodes the same format, and writes it to a stream supplied and owned by the caller. It does not resize, allocate the destination stream, reset its position or truncate old contents.
The example is fictional and source-reviewed only. No image function or codec was executed.
The out contract is defective
Although the public declaration says out, the native body immediately reads Assigned(aNewImageStream) and later calls methods on that existing object. It never creates or assigns a stream. Modified PascalScript passes the existing variable by address, so scripts must construct it before calling, exactly as the example shows.
Treat the parameter operationally as var:
- caller creates the stream;
- caller ensures it is writable and, where required, seekable/resizable;
- caller selects its starting
Positionand clears/truncates it if necessary; - caller remains responsible for freeing it, including after
Falseor an exception.
Do not pass an uninitialized/nil variable expecting this function to return a new stream; it returns False.
Implementation trace
- Return false if the source file does not exist or the destination reference is nil.
- Select
TJPEGImagefrom.jpg/.jpegorTPngImagefrom.png. - Load the complete source file.
- Set JPEG quality and force
Compress, or assign the PNGMapTo10compression level. - Call
lImage.SaveToStream(aNewImageStream)at the destination's current position. - Return true and free only the temporary graphic.
Stream position, size and ownership
SaveToStream writes from the destination's current Position. Velox does not set position zero, set size zero or remove any bytes beyond the newly written image. Consequences include:
- a new empty
TMemoryStreamreceives a single image starting at byte 0; - a nonzero position prefixes the image with existing data;
- writing over a longer previous image can leave a stale tail unless the caller truncates first;
- after success, the destination position is advanced by the encoded bytes;
- on exception, the stream can contain a partial image at an advanced position;
- the same stream may be reused only with explicit caller position/size management.
The function never frees the destination. Its out mode does not transfer ownership.
Format and quality behavior
The source extension chooses the encoder, so output bytes have the source format. There is no destination filename to request conversion.
- JPEG is lossy and assigns
aQualitydirectly to the VCL1..100property. Current range-checked builds raiseERangeErrorbelow 1 or above 100. - PNG is lossless.
MapTo10maps negative..9 to 1, 10..89 to 2..9 and every input 90 or greater to 10. Current range-checked builds raiseERangeErrorbecause the installed property is0..9; level 0 is unreachable.
Higher PNG values do not improve visual fidelity. Treat PNG input 90 or greater as a source-proven defect pending the future test-bed/product stream.
Unsupported-extension defect
For an existing source whose suffix is not JPEG/PNG, the native local graphic pointer is uninitialized. Execution still dereferences and frees it. This is invalid-pointer behavior, commonly an access violation, rather than a reliable False. A recognized suffix with corrupt or mismatched bytes raises from the selected decoder.
Side effects
Reads/decodes a source file and mutates the destination stream from its current position. It does not modify the source file or close either caller resource.
Performance, limits and concurrency
The entire decoded image and encoded result are processed in memory. There are no source-size/dimension limits. A caller-provided stream may block or fail on write. VCL codec thread safety is not established by this wrapper; avoid unsynchronized concurrent use in a shared hosting context.
Related entries
CompressImageFileToFilewrites destructively to a path.CompressImageStreamToStreamtakes image bytes from a seekable stream.ResizeImageFileToStreamproportionally resizes before same-format stream output.SaveStreamToFilecan persist a completed caller-owned stream under its separate contract.
External references
- Embarcadero
Vcl.Imaging.jpeg.TJPEGImage - Embarcadero
Vcl.Imaging.pngimage.TPngImage - Embarcadero
TStream.Position - Free Pascal
TStream- compatible stream position/size/ownership context; Velox uses installed Delphi/VCL.