我正在构建一个程序,该程序生成图像,并通过REST调用将它们作为base64编码的字符串发送到网页。所有的图像都是生成和拉入的,没有问题,除了一个迄今为止。
我已经渲染了一个图像,尽管我努力用它进行编码/解码循环,但它似乎无法解码。我尝试过使用。NET、Python和各种base64验证网站,以确定我是否可以正确解码由该图像生成的编码字符串,我对可能发生的事情感到不知所措。图像将保存一个.png的结果,该结果看起来是正确的,但任何将位图转换为base64编码字符串的尝试都将导致一个似乎无法加载到图像中的字符串。该字符串被验证为正确的base64编码,编码过程在所有其他情况下都能正常工作,但我需要知道正在渲染的图像有什么不正确的地方,以及为什么它不能作为编码数据工作。
渲染这些图像的过程是从文件中提取原始图像,并根据用户参数进行裁剪和缩放。新更改的文件被编码为base64字符串,并作为REST调用的结果发送。
这是我在ASP中使用的代码。NET核心:
private string GetResizedMapString(Map map, CanvasDisplaySettings disp)
{
string path = GetUploadPath(map.SourceName);
if (path != string.Empty)
{
Image srcImg = Image.FromFile(path);
RectangleF[] imgSizes = GetRenderAreas(srcImg, map, disp);
if (imgSizes != null)
{
Bitmap destImg = new((int)imgSizes[0].Width, (int)imgSizes[0].Height);
using var graphics = Graphics.FromImage(destImg);
graphics.DrawImage(srcImg, imgSizes[0], imgSizes[1], GraphicsUnit.Pixel);
// CREATING TESTING IMAGE AND ENCODED STRING FILES:
string testImgName = "TestImgFrom" + disp.DisplayID;
string filePath = Path.Combine(DepoCollection, testImgName);
String imageString = ConvertBitmapToString(destImg); // New Image String
String imageStringPath = filePath + ".txt"; // New Encoded String File
String imagePngPath = filePath + ".png"; // New Image File
// Save to PNG
destImg.Save(imagePngPath);
graphics.Dispose();
// Save to .txt to test the encoded string
using (StreamWriter outputFile = new StreamWriter(imageStringPath))
{
outputFile.WriteLine("data:image/png;base64," + imageString);
}
return imageString;
}
else return "";
}
}
static string ConvertBitmapToString(Bitmap bitmap)
{
// Convert the bitmap to a byte array
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byteImage = ms.ToArray();
// Convert the byte array to a base64 string
string base64String = Convert.ToBase64String(byteImage);
return base64String;
}
这个过程已经用于许多其他图像,并且缩放之前的原始图像能够被编码/解码。我尝试过使用Python进行类似的编码/解码过程,并将图像加载到在线的其他base64编码软件中。
程序生成的图像比源图像大得多,所以我不能在这里上传。我想知道为什么正在发生的事情会使文件大小增加这么多,以及这是否可能是相关的。
源图像:1.34MB,2200 x 2200像素
渲染图像:24.00 MB,2998 x 2998像素
以下是渲染文件的Google Drive链接:
https://drive.google.com/file/d/1wxkWCEx3DyUG2yenWoEfOsi9QNEK38FK/view?usp=sharing
感谢您提供有关正在发生的事情的任何线索以及如何避免这些问题的任何建议!