locked
Download image attachment to be viewed on ASPX RRS feed

  • Question

  • Hello there,

    I am using crm 4.0

    I want to download an image that is actually attached to some record, so I can be able to view it on an image control on aspx page.

    the image format is JPEG and the the image is attached to a custom entity.

    I took this code from the sdk as follow:

    Guid annotationId = new Guid("{BF19F391-CFFA-DF11-9D28-000E7F5F207C}");
    
     // Define the columns to retrieve from the annotation record.
    
     ColumnSet cols1 = new ColumnSet();
     cols1.Attributes = new string[] { "filename", "documentbody" };
    
     // Retrieve the annotation record.
    
     annotation annotationAttachment = (annotation)_service.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);
    
     // Download the attachment in the current execution folder.
    
     using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
     {
    
      byte [] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
      fileStream.Write(fileContent, 0, fileContent.Length);
    
     }
    
     
    

     

    till here everything works great, but when i try to convert from byte[] to an image i get this exception error(Parameter is not valid)

    this is the code that converts from byte[] to image

     

    MemoryStream ms = new
     MemoryStream();
    ms.Write(fileContent , 0, fileContent .Length);
    using (System.Drawing.Image img = System.Drawing.Image.FromStream(ms))
    {
    img.Save("foo.jpg" , ImageFormat.Jpeg);
    }
    how can i solve this problem?

     

    Sunday, November 28, 2010 12:12 PM

Answers

  • You need to convert the document body from a base64 string, here is a function that will take your string and convert it to an Image object

    //Base64 String to Image
    public Image Base64ToImage(string base64String)
    {
      // Convert Base64 String to byte[]
      byte[] imageBytes = Convert.FromBase64String(base64String);
      MemoryStream ms = new MemoryStream(imageBytes, 0,
        imageBytes.Length);


      // Convert byte[] to Image
      ms.Write(imageBytes, 0, imageBytes.Length);
      Image image = Image.FromStream(ms, true);
      return image;


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Check out the CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM Forum Guidance on how to Help Us Help You
    • Marked as answer by Ahmad Anwar Sunday, November 28, 2010 12:55 PM
    Sunday, November 28, 2010 12:38 PM
    Moderator

All replies

  • You need to convert the document body from a base64 string, here is a function that will take your string and convert it to an Image object

    //Base64 String to Image
    public Image Base64ToImage(string base64String)
    {
      // Convert Base64 String to byte[]
      byte[] imageBytes = Convert.FromBase64String(base64String);
      MemoryStream ms = new MemoryStream(imageBytes, 0,
        imageBytes.Length);


      // Convert byte[] to Image
      ms.Write(imageBytes, 0, imageBytes.Length);
      Image image = Image.FromStream(ms, true);
      return image;


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Check out the CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM Forum Guidance on how to Help Us Help You
    • Marked as answer by Ahmad Anwar Sunday, November 28, 2010 12:55 PM
    Sunday, November 28, 2010 12:38 PM
    Moderator
  • Thank you very very much.

    I was stuck in this problem for about 2 days.

    Everything works as expected.

    Thank you again.

    Sunday, November 28, 2010 12:56 PM