我正在从报告服务引擎中以字节数组的形式获取图像。
byte[] imageBytes = Convert.FromBase64String(base64);//base64 is an byte array
我正在将此字节数组转换为图像
MemoryStream ms1 = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms1.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image images = System.Drawing.Image.FromStream(ms1, true);**
但当我保存它时,它只存储第一个图像
images.Save(Server.MapPath("~/img.png"),System.Drawing.Imaging.ImageFormat.Png);
它必须保存两个图像。我无法检测base64第一个图像或第二个图像
您只保存了该字节数组中的一个图像。PNG不包含多个图像,所以你必须知道在哪里"分割"base64,这样你才能得到两个图像。请参阅下面的伪代码,了解我要说的内容:
int i = 1;
string split = "//**//";
// the string "//**//" was inserted so we know where to split the string to
// extract mutiple images. It can be any string value
foreach (var b64str in base64.Split(split))
{
byte[] imageBytes = Convert.FromBase64String(b64str);//base64 is an byte array
MemoryStream ms1 = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms1.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image images = System.Drawing.Image.FromStream(ms1, true);
images.Save(Server.MapPath("~/img" + i.ToString() + ".png"),System.Drawing.Imaging.ImageFormat.Png);
i++;
}
我解决了这个URL的问题如何在<img/>标签
在页面底部,他们提供了运行良好的解决方案。。。。
首先-从图像流中获取所有帧作为图像列表
public List<Image> GetAllFrames(Stream sm)
{
List<Image> images = new List<Image>();
Bitmap bitmap = new Bitmap(sm);
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
bitmap.SelectActiveFrame(FrameDimension.Page, idx);
MemoryStream byteStream = new MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);
images.Add(Image.FromStream(byteStream));
}
return images;
}
第二个-将所有帧合并为一个位图。
public Bitmap CombineAllFrames(List<Image> test)
{
int width = 0;
int height = 0;
Bitmap finalImage = null;
try
{
foreach (Bitmap bitMap in test)
{
height += bitMap.Height;
width = bitMap.Width > width ? bitMap.Width : width;
}
finalImage = new Bitmap(width, height);
using (System.Drawing.Graphics gc = Graphics.FromImage(finalImage))
{
gc.Clear(Color.White);
int offset = 0;
foreach (Bitmap bitmap in test)
{
gc.DrawImage(bitmap, new Rectangle(0, offset, bitmap.Width, bitmap.Height));
offset += bitmap.Width;
}
}
}
catch (Exception)
{
throw;
}
return finalImage;
}
这种方法创建一个位图,将所有帧垂直附加到一个位图中。如果你想水平更新到
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
g.DrawImage(image, new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
第三步-现在,如果你想为创建的图像调用一个字节数组,请调用以下方法
public byte[] GetBytesFromImage(Bitmap finalImage)
{
ImageConverter convertor = new ImageConverter();
return (byte[])convertor.ConvertTo(finalImage, typeof(byte[]));
}