PDF Image Crash

Hi,
There is no problem when listing pictures in the datagrid, but when I try to print it, I get an error.
By the way when I remove the picture from the grid works fine.

Not : I added the errors and codes to the attached file

Attachment: Arşiv_7041545d.zip

6 Replies 1 reply marked as answer

PK Pradeep Kumar Balakrishnan Syncfusion Team January 12, 2021 03:56 PM UTC

Hi Tayyip, 
 
Thank you contacting Syncfusion support.  
 
We have checked your requirement. “How to export SfDataGrid with image column in Xamarin forms”. SfDataGrid does not have direct support to export image. However, we can achieve your requirement by using CellExporting of DataGridPdfExportingController. Please refer the following code snippet for reference. 
 
Code Snippet: 
 
private void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "Image") 
    { 
        var style = new PdfGridCellStyle(); 
        // ImageSource source = ImageSource.FromResource("DataGridDemo.Images." + e.CellValue.ToString(), typeof(MainPage).GetTypeInfo().Assembly); 
 
        var streamImageSource = e.CellValue as StreamImageSource; 
        if (streamImageSource != null) 
        { 
            var imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream(ExportingHelper.GetImagePath(streamImageSource)); 
 
            if (imageStream != null) 
            { 
                PdfImage pdfImage = PdfImage.FromStream(imageStream); 
                style.BackgroundImage = pdfImage; 
                e.PdfGridCell.ImagePosition = PdfGridImagePosition.Center; 
                e.PdfGridCell.Style = style; 
                imageStream.Flush(); 
                e.CellValue = null; 
            } 
        } 
    } 
} 
 
We have also attached sample for your reference in the following link  
 
Let us know if you need any further assistance on this. 
 
Regards, 
Pradeep Kumar B 



TE Tayyip Emre ÖRNEK replied to Pradeep Kumar Balakrishnan January 13, 2021 10:11 AM UTC

Hi Tayyip, 
 
Thank you contacting Syncfusion support.  
 
We have checked your requirement. “How to export SfDataGrid with image column in Xamarin forms”. SfDataGrid does not have direct support to export image. However, we can achieve your requirement by using CellExporting of DataGridPdfExportingController. Please refer the following code snippet for reference. 
 
Code Snippet: 
 
private void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "Image") 
    { 
        var style = new PdfGridCellStyle(); 
        // ImageSource source = ImageSource.FromResource("DataGridDemo.Images." + e.CellValue.ToString(), typeof(MainPage).GetTypeInfo().Assembly); 
 
        var streamImageSource = e.CellValue as StreamImageSource; 
        if (streamImageSource != null) 
        { 
            var imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream(ExportingHelper.GetImagePath(streamImageSource)); 
 
            if (imageStream != null) 
            { 
                PdfImage pdfImage = PdfImage.FromStream(imageStream); 
                style.BackgroundImage = pdfImage; 
                e.PdfGridCell.ImagePosition = PdfGridImagePosition.Center; 
                e.PdfGridCell.Style = style; 
                imageStream.Flush(); 
                e.CellValue = null; 
            } 
        } 
    } 
} 
 
We have also attached sample for your reference in the following link  
 
Let us know if you need any further assistance on this. 
 
Regards, 
Pradeep Kumar B 


Thank you for answer.

But the same error continues. All grid are full

Value cannot be null.
Parameter name: name


PK Pradeep Kumar Balakrishnan Syncfusion Team January 14, 2021 09:22 AM UTC

Hi Tayyip, 
 
Thank you for the update. 
 
We have checked your update this issue will occur when exporting image column, please check the provided code snippet and sample to overcome this issue and ensure following highlighted code is executed. 
 
private void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e)  
{  
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "Image")  
    {  
        var style = new PdfGridCellStyle();  
        // ImageSource source = ImageSource.FromResource("DataGridDemo.Images." + e.CellValue.ToString(), typeof(MainPage).GetTypeInfo().Assembly);  
  
        var streamImageSource = e.CellValue as StreamImageSource;  
        if (streamImageSource != null)  
        {  
            var imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream(ExportingHelper.GetImagePath(streamImageSource));  
  
            if (imageStream != null)  
            {  
                PdfImage pdfImage = PdfImage.FromStream(imageStream);  
                style.BackgroundImage = pdfImage;  
                e.PdfGridCell.ImagePosition = PdfGridImagePosition.Center;  
                e.PdfGridCell.Style = style;  
                imageStream.Flush();  
                e.CellValue = null;  
            }  
        }  
    }  
}  
This set of code will not be executed only when image is not there in respective place. Ensure to add the image in your PCL project and BuildAction should be Embedded resource.  
 
Kindly check the attached sample in the previous update. 
 
Regards, 
Pradeep Kumar B 


Marked as answer

TE Tayyip Emre ÖRNEK January 14, 2021 10:09 AM UTC

Sorry my fault.
Images are from url. How can I do that way?


PK Pradeep Kumar Balakrishnan Syncfusion Team January 17, 2021 03:44 PM UTC

Hi Tayyip, 
 
Thank you for the update. 
 
Currently, we are validating to achieve your requirement “How to export datagrid image solum when images are from URL in Xamarin forms”. We will validate and update further details by January 20, 2021. We appreciate your patience until then. 
 
Regards, 
Pradeep Kumar B 



PK Pradeep Kumar Balakrishnan Syncfusion Team January 20, 2021 05:17 PM UTC

Hi Tayyip, 
 
Thank you for your patience. 
 
We have to download the images from the respective URL and use those images to export. In the following code snippet, we downloaded and stored as byte array in DataGrid model class and created a memory stream from using those byte arrays. 
 
Code Snippet: 
Code to get byte array from URL 
 
async Task<byte[]> DownloadImageAsync(string imageUrl) 
{ 
    try 
    { 
        using (var httpResponse = await httpClient.GetAsync(imageUrl)) 
        { 
            if (httpResponse.StatusCode == HttpStatusCode.OK) 
            { 
                return await httpResponse.Content.ReadAsByteArrayAsync(); 
            } 
            else 
            { 
                //Url is Invalid 
                return null; 
            } 
        } 
    } 
    catch (Exception e) 
    { 
        //Handle Exception 
        return null; 
    } 
} 
 
 
ImageArrary = await DownloadImageAsync("https://www.syncfusion.com/downloads/support/directtrac/general/logo2059051212"), 
 
private async void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "Image") 
    { 
        var style = new PdfGridCellStyle(); 
        byte[] bytes = ((e.Record as RecordEntry).Data as OrderInfo).ImageArrary; 
 
        Stream imageStream = null; 
        if (bytes != null) 
        { 
            imageStream = new MemoryStream(bytes); 
        } 
 
        if (imageStream != null) 
        { 
                PdfImage pdfImage = PdfImage.FromStream(imageStream); 
                style.BackgroundImage = pdfImage; 
                e.PdfGridCell.ImagePosition = PdfGridImagePosition.Center; 
                e.PdfGridCell.Style = style; 
                imageStream.Flush(); 
                e.CellValue = null; 
        } 
    } 
} 
 
We have also attached sample for your reference in the following link. 
 
Let us know if you need any further assistance on this. 
 
Regards, 
Pradeep Kumar B 


Loader.
Up arrow icon