Base64 is one of the most common encoding methods on the network for transmitting 8Bit bytecode. Base64 is a method of representing binary data based on 64 printable characters. You can view RFC2045~RFC2049, which contains detailed MIME specifications.
Base64 encoding is a process from binary to character and can be used to pass longer identification information in an HTTP environment. Base64 encoding is unreadable and needs to be decoded before reading.
Base64 is widely used in various fields of computers due to the above advantages. However, since the output content includes more than two "symbol class" characters (+, /, =), various "variants" of Base64 have been developed in different application scenarios. To unify and normalize the output of Base64, Base62x is considered an improved version of unsigned.
The following is a code to introduce the mutual conversion problem of c# images and base64. The code is as follows:
public ActionResult UploadSignature2(string src_data) { Class1.Base64StrToImage(src_data, "C:\\Users\\45448\\Desktop\\1\\"+ ("yyyyMMddHHss") + ".png"); return Json(1, ); } /// <summary> /// Convert Base64 string to image and save to local /// </summary> /// <param name="base64Str">base64 string</param> /// <param name="savePath">Image save address, such as: /Content/Images/</param> /// <returns></returns> public static bool Base64StrToImage(string base64Str, string savePath) { var ret = true; try { base64Str = ("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); //Replace base64 header information var bitmap = Base64StrToImage(base64Str); if (bitmap != null) { //Create a folder var folderPath = (0, ('\\')); ////(folderPath); if (!(folderPath)) { (folderPath); } //Image suffix format var suffix = (('.') + 1, - ('.') - 1).ToLower(); var suffixName = suffix == "png" ? : suffix == "jpg" || suffix == "jpeg" ? : suffix == "bmp" ? : suffix == "gif" ? : ; //Copy a copy here to save the image, otherwise the error message "a general error occurred in GDI+" will appear var bmpNew = new Bitmap(bitmap); (savePath, suffixName); (); (); } else { ret = false; } } catch (Exception ex) { ret = false; } return ret; }
/// <summary> /// Image converted to base64 /// </summary> /// <param name="fileFullName"></param> public static string ImageToBase64(string fileFullName) { try { Bitmap bmp = new Bitmap(fileFullName); MemoryStream ms = new MemoryStream(); var suffix = (('.') + 1, - ('.') - 1).ToLower(); var suffixName = suffix == "png" ? : suffix == "jpg" || suffix == "jpeg" ? : suffix == "bmp" ? : suffix == "gif" ? : ; (ms, suffixName); byte[] arr = new byte[]; = 0; (arr, 0, (int)); (); return Convert.ToBase64String(arr); } catch (Exception ex) { return null; } }
This is the article about the mutual conversion of C# pictures and Base64 codes (detailed explanation of the code). For more related contents of C# pictures and base64, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!