GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / AddJpegImageFromStream Method / AddJpegImageFromStream(Stream) Method
A Stream object, a source of your image. Supported file format is JPEG (jpg, jpeg, jpe ...).

This stream object must be initialized before it can be sent into this method and it should remain open for subsequent use.

Example





In This Topic
AddJpegImageFromStream(Stream) Method
In This Topic
Creates an image resource directly from your JPEG image stored in the instantiated Stream object without the need of lossy encoding and decoding process. The image resource is subsequently added into the currently loaded PDF document. You can use this resource straightforward, for example, with the DrawImage method. This process allows you to easily draw an image from your JPEG source file onto a page within the currently loaded PDF document.
Syntax
'Declaration
 
Public Overloads Function AddJpegImageFromStream( _
   ByVal Stream As Stream _
) As String
public string AddJpegImageFromStream( 
   Stream Stream
)
public function AddJpegImageFromStream( 
    Stream: Stream
): String; 
public function AddJpegImageFromStream( 
   Stream : Stream
) : String;
public: string* AddJpegImageFromStream( 
   Stream* Stream
) 
public:
String^ AddJpegImageFromStream( 
   Stream^ Stream
) 

Parameters

Stream
A Stream object, a source of your image. Supported file format is JPEG (jpg, jpeg, jpe ...).

This stream object must be initialized before it can be sent into this method and it should remain open for subsequent use.

Return Value

Returns a name of the newly inserted image resource strictly bounded to the currently loaded PDF document. The GetStat method can be subsequently used to determine if this method has been successful.

This name can be subsequently used with the DrawImage method to draw an image onto the page. For the correct usage please see the example below.

Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GetStat method to identify the specific reason for the method's failure, if any.

Also be aware that the input stream should remain open and can only be closed/disposed of by the user.

Example
How to draw a JPEG image stored in the instantiated stream object onto a new page and save it to a PDF file.
Dim caption As String = "Example: AddJpegImageFromStream"
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.NewPDF()
If status = GdPictureStatus.OK Then
    Dim image_stream As System.IO.Stream = New System.IO.FileStream("image.jpg", System.IO.FileMode.Open)
    Dim image_name As String = gdpicturePDF.AddJpegImageFromStream(image_stream)
    status = gdpicturePDF.GetStat()
    If status = GdPictureStatus.OK Then
        If gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK Then
            gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft)
            gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
            If gdpicturePDF.DrawImage(image_name, 0, 0, 210, 297) = GdPictureStatus.OK Then
                status = gdpicturePDF.SaveToFile("Test_AddJpegStream.pdf")
                If status = GdPictureStatus.OK Then
                    MessageBox.Show("Your image has been successfully saved to a pdf file.", caption)
                Else
                    MessageBox.Show("The file can't be saved. Status: " + status.ToString(), caption)
                End If
            End If
        End If
    Else
        MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + status.ToString(), caption)
    End If
    gdpicturePDF.CloseDocument()
    image_stream.Close()
Else
    MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: AddJpegImageFromStream";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.NewPDF();
if (status == GdPictureStatus.OK)
{
    System.IO.Stream image_stream = new System.IO.FileStream("image.jpg", System.IO.FileMode.Open);
    string image_name = gdpicturePDF.AddJpegImageFromStream(image_stream);
    status = gdpicturePDF.GetStat();
    if (status == GdPictureStatus.OK)
    {
        if (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK)
        {
            gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);
            gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
            if (gdpicturePDF.DrawImage(image_name, 0, 0, 210, 297) == GdPictureStatus.OK)
            {
                status = gdpicturePDF.SaveToFile("Test_AddJpegStream.pdf");
                if (status == GdPictureStatus.OK)
                {
                    MessageBox.Show("Your image has been successfully saved to a pdf file.", caption);
                }
                else
                {
                    MessageBox.Show("The file can't be saved. Status: " + status.ToString(), caption);
                }
            }
        }
    }
    else
    {
        MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + status.ToString(), caption);
    }
    gdpicturePDF.CloseDocument();
    image_stream.Close();
}
else
{
    MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption);
}
gdpicturePDF.Dispose();
See Also