C# 常用方法——圖片轉base64字符串


其他擴展方法詳見:https://www.cnblogs.com/zhuanjiao/p/12060937.html

        /// <summary>
		/// Image 轉成 base64
		/// </summary>
		/// <param name="fileFullName"></param>
		public string ImageToBase64(string fileFullName)
		{
			try
			{
				Bitmap bmp = new Bitmap(fileFullName);
				MemoryStream ms = new MemoryStream();
				var suffix = fileFullName.Substring(fileFullName.LastIndexOf('.') + 1,
					fileFullName.Length - fileFullName.LastIndexOf('.') - 1).ToLower();
				var suffixName = suffix == "png"
					? ImageFormat.Png
					: suffix == "jpg" || suffix == "jpeg"
						? ImageFormat.Jpeg
						: suffix == "bmp"
							? ImageFormat.Bmp
							: suffix == "gif"
								? ImageFormat.Gif
								: ImageFormat.Jpeg;

				bmp.Save(ms, suffixName);
				byte[] arr = new byte[ms.Length]; ms.Position = 0;
				ms.Read(arr, 0, (int)ms.Length); ms.Close();
				return Convert.ToBase64String(arr);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM