我正在降低图像的质量并将其保存到文件系统中。我希望以 Base64 字符串格式返回图像的新数据。我怎样才能做到这一点?
代码:
var bitmap = new Bitmap(Base64ToImage(base64String));
var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
var myEncoder = Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, Convert.ToInt64(75L));
myEncoderParameters.Param[0] = myEncoderParameter;
bitmap.Save(filePath, jgpEncoder, myEncoderParameters);
更新:
以下是合适的方法吗?
// Convert the image to byte[]
var stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Jpg);
var imageBytes = stream.ToArray();
// Convert byte[] to Base64 String
var reducedQuality = Convert.ToBase64String(imageBytes);
return reducedQuality;