c#中将图像保存在特定位置的问题

本文关键字:定位 位置 问题 存在 图像 保存 | 更新日期: 2023-09-27 18:19:16

我能够将图像转换为base64字符串和base64字符串转换为图像。我将图像保存到一个特定的位置。如果我第一次运行,我得到了作为输入的正确的映像文件。但是从第二次运行中,不管我传递的输入图像是什么,我只得到第一个输入图像。

期望输出:我需要得到我传递的输入图像。

转换码

public  string ImageToBase64(Image image, ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();
        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
    }
}
public  Image Base64ToImage(string base64String)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes, 0,
      imageBytes.Length);
    // Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = Image.FromStream(ms, true);
    return image;
}

保存文件代码:

public void retreiveAndSaveImgFile(string base64formofstr)
{
    Console.WriteLine("the incoming string :: " + base64formofstr.Length);
/*    Bitmap bitmap = new Bitmap(Base64ToImage(base64formofstr));
    Bitmap newBitmap = new Bitmap(bitmap);
    newBitmap.SetResolution(150, 150); // resolution of the original image for which the zone template is created
    newBitmap.Save("D:''Suraya''TesttogetCorrectPutput''MICR_SAMPLE.tif", ImageFormat.Tiff);*/
    Base64ToImage(base64formofstr).Save("D:''Suraya''TesttogetCorrectPutput''MICR_SAMPLE.tif");
}

请帮我解决这个问题。

c#中将图像保存在特定位置的问题

我不是那么聪明与这一切,但直到我明白你的问题是因为缓存…由于您一直使用相同的名称,因此它将显示来自缓存的输出,而不是转换。

First Alternate是为图像的名称添加时间戳。
第二种选择是强制浏览器重新加载整个页面,而不是从缓存中显示。