Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
normally this code works, but when we create a project with this code and then change the language option from windows region and language settings, the image cannot be loaded.
In short, the image save feature gives an error when the windows language option is changed

C#
ImageToBase64(Image image)
image.Save(m,image.RawFormat);

Base64ToImage(String base64String)
image = System.Drawing.Image.FromStream(ms, true);


What I have tried:

this change setting is problem load image

Decimal symbol and Dijit grouping symbol change after load image error

C#
public static String ImageToBase64(Image image)
{
   if (image == null)
   {
       return "";
   }
   using (MemoryStream m = new MemoryStream())
   {
       image.Save(m,image.RawFormat);

       byte[] imageBytes = m.ToArray();

       string base64String;

       if (int.Parse(CSabit.filevers) > 1)

           base64String    = ByteArrayToString(imageBytes);
       else
           base64String= Convert.ToBase64String(imageBytes,Base64FormattingOptions.InsertLineBreaks);
       return base64String;
   }
}

public static Image Base64ToImage(String base64String)
{
   string adr = Path.GetDirectoryName(FrmMain.ProjeAdresi);
   byte[] imageBytes;
   if (base64String == "")
       return null;


   if (int.Parse(CSabit.filevers) > 1)
   {
       imageBytes = StringToByteArray(base64String);

   }

   else
   {

       if (DebugMode == "2")
       {
           if (!File.Exists(adr + @"\resim\" + base64String)) return null;

           imageBytes = File.ReadAllBytes(adr + @"\resim\" + base64String); // Convert.FromBase64String(base64String);
       }
       else
       {
           imageBytes = Convert.FromBase64String(base64String);

       }

   }

   MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

   ms.Write(imageBytes, 0, imageBytes.Length);
   System.Drawing.Image image;

   try
   {
       image = System.Drawing.Image.FromStream(ms, true);
   }
   catch
   {

       image = Image.FromFile("imr.png");
   }

   return image;
}
Posted
Updated 27-Apr-23 0:37am
v3
Comments
Richard MacCutchan 26-Apr-23 12:23pm    
Where is the code for the ImageToBase64 and Base64ToImage methods?
[no name] 26-Apr-23 14:12pm    
Maybe you should say what you want to accomplish; since there seems to be no point to the code you've shown (and I've used images "a lot").
Dave Kreskowiak 27-Apr-23 8:05am    
Decimal symbol and Dijit grouping symbol change after load image error
Whatever that means. You might want to post the EXACT error message and the line on which it occurs.
Dave Kreskowiak 27-Apr-23 8:07am    
Your code is confusing to say the least. A method called "Base64ToImage" should NOT be converting a string to a value, getting file paths, converting a string to a byte array, ... nothing other than accepting a base64 string and returning a Bitmap object. That's it!
Richard MacCutchan 27-Apr-23 8:53am    
"the image save feature gives an error when the windows language option is changed"
What error and where does it occur?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900