0

I am converting image from bytes by FromBase64String method in asp.net C#, but at that time it showing error i.e. System.FormatException: 'Invalid length for a Base-64 char array or string.' in web method.

Here is my code,

[WebMethod]
public static List<CustomerMortgageModel> GetProductList()
{
    string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

    List<CustomerMortgageModel> customers = new List<CustomerMortgageModel>();
    Service service = new Service();

    using (SqlConnection con = new SqlConnection(constr))
    {
        string qrySelProductDetail = "select * from tbl_MortageDetail " + System.Environment.NewLine;
        
        using (SqlCommand cmd = new SqlCommand(qrySelProductDetail, con))
        {
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    // Setup image and get data stream together
                    System.Drawing.Image img;
                    System.IO.MemoryStream MS = new System.IO.MemoryStream();
                    string b64 = sdr["DesignImage"].ToString().Replace(" ", "+");
                    byte[] b;

                    // Converts the base64 encoded msg to image data
                    b = Convert.FromBase64String(b64);
                    MS = new System.IO.MemoryStream(b);

                    // creates image
                    img = System.Drawing.Image.FromStream(MS);

                    customers.Add(new CustomerMortgageModel
                    {
                             DesignImage = img
                    });
                }
            }
            con.Close();
        }
        
    }
    return customers;
}

BackEnd Code

<asp:GridView ID="gv_productdetail" runat="server" CssClass="display compact" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Image">  
            <ItemTemplate>  
                <img src="data:image/jpeg;base64,<%# Eval("DesignImage") %>" />
            </ItemTemplate>  
        </asp:TemplateField>  

    </Columns>
</asp:GridView>
s.k.Soni
  • 1,139
  • 1
  • 19
  • 37
  • Does this answer your question? [c# base64 encrpytion. Invalid length error on decrpytion](https://stackoverflow.com/questions/44968392/c-sharp-base64-encrpytion-invalid-length-error-on-decrpytion) – jps Jul 09 '21 at 10:52
  • @jps I tried but didnt work, showing same error – s.k.Soni Jul 09 '21 at 11:35
  • Can you show the value of `string b64`? Without seeing the actual data it's hard to tell what's wrong. – jps Jul 09 '21 at 11:42
  • @jps here is the value of b64 – s.k.Soni Jul 09 '21 at 11:51
  • VBORw0KGgoAAAANSUhEUgAAEEAAAAYQCAIAAADkox+PAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7N0FW – s.k.Soni Jul 09 '21 at 11:55
  • @jps and continues goes on – s.k.Soni Jul 09 '21 at 11:55
  • 1. looks like a normal base64 string 2. maybe it's just a mistake when you copied it here, but at least an `i` is missing on the beginning. A base64 encoded PNG image always starts with `iVBORw0KGg` 3. as you don't show the complete string, I can't judge if padding is missing, but if you apply the solution in the linked answer, it will add padding automatically if it's missing. And then there should be no ' Invalid length' error anymore. – jps Jul 09 '21 at 12:05
  • You didn't give any feedback anymore. Have you been able to solve it? – jps Jul 10 '21 at 10:03
  • @jps Nopes. Still problem is coming but now new problem coming. Please review my next question related to this question in the following link https://stackoverflow.com/questions/68326654/in-the-place-of-image-showing-object-object-when-convert-bytes-to-image-in-asp – s.k.Soni Jul 10 '21 at 10:07

0 Answers0