【问题标题】:How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c#如何在 asp.net core 1.0 c#中将字符串(实际上是位图)转换为 IFormFile
【发布时间】:2020-10-27 11:25:26
【问题描述】:

基本上我要做的是发送一个位图字符串(我转换为字符串)并将 t 发送到 Asp.net core 1.0 web application/api。我想将该图像保存到托管应用程序文件中,并将该图像的路径保存到数据库中,但我不明白如何将该字符串转换为 IFormFile,以便将其复制到特定路径。

        List<byte> splitBytes = new List<byte>();
        string byteString = "";

        foreach (var chr in obj.bitmapstr)
        {
            byteString += chr;

            if (byteString.Length == 3)
            {
                splitBytes.Add(Convert.ToByte(byteString));
                byteString = "";
            }
        }

        if (byteString != "")
            splitBytes.AddRange(Encoding.ASCII.GetBytes(byteString));

        using (var ms = new MemoryStream(splitBytes.ToArray()))
        {

           

             // SixLabors.ImageSharp.Image img = SixLabors.ImageSharp.Image.FromStream()

            //  ms.Write(splitBytes.ToArray(), 0, splitBytes.ToArray().Length);


            //   var img = System.Drawing.Image.FromStream(ms);

            //do something with image.
        }

我尝试将其转换为字节数组,但不知道如何处理。

             Uri path=data.getData();
               
            Bitmap bitmap=MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),path);
            phtotimage.setImageBitmap(bitmap);
             phtotimage.setVisibility(View.VISIBLE);
            String bitMapToString=BitMapToString(bitmap);
           getconnection("url",bitMapToString);

这是android studio中获取文件并将其转换为字符串并传递该文件的代码

【问题讨论】:

  • 你应该使用 Base64 来编码你的图像
  • 图像在 Base64 中已经准备好我无法将其转换为 IFormFile 或 image 。我见过很多他们使用 Image.FromStream 的代码,但在我的情况下 Image.FromStream FromStream 不存在

标签: java c# asp.net-mvc asp.net-core asp.net-core-webapi


【解决方案1】:

位图

bitmap 是一种用于存储数字图像的内存组织或图像文件格式。术语位图来自计算机编程术语,意思是a map of bits,一个空间映射的位数组。现在,与 pixmap 一起,它通常指代像素空间映射数组的类似概念。
它是"raw" 图像文件,其中存储没有headersize 或其他信息的原始位图。

IFormFile

同时,IFormFile 具有以下结构:

public interface IFormFile
{
    string ContentType { get; }
    string ContentDisposition { get; }
    IHeaderDictionary Headers { get; }
    long Length { get; }
    string Name { get; }
    string FileName { get; }
    Stream OpenReadStream();
    void CopyTo(Stream target);
    Task CopyToAsync(Stream target, CancellationToken cancellationToken = null);
}

从这里,您可以看到String(=Bitmap) 和IFormFile 之间的区别。 所以这不会像你期望的那样转化成功。

将base64字符串转换为FormFile

using (var stream = System.IO.File.WriteAllBytes(@"c:\yourfile", Convert.FromBase64String(bitmapstr)))
        {
            var formFile = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
            {
                Headers = new HeaderDictionary(),
                ContentType = "YOUR CONTENT TYPE HERE"
            };
        }

【讨论】:

  • 你能告诉我我们如何在 volley 中发送这种类型的请求...因为我正在使用 volley ......实际上在第 (2) 部分发送的内容...a String 或 MultipartBody 文件或两者兼有............在第 (1) 部分中,“yourfile”是什么意思......我要存储图像的地方?
  • @Ahmed Ishtiaq 我认为解决方案 2 不适合您的情况。它有点复杂和无用。让我们从服务器本地路径的 str/byte[] 中保存位图。
  • public static void WriteAllBytes (string path, byte[] bytes);是的,“yourfile”是[路径]将文件保存到本地文件系统。
  • 是的,正如你所说如果我直接将 bitmapstr 保存到数据库中会很容易......所以是的,这很容易......谢谢@Michelle Wang .....我真的很感激。
  • 内容类型会有什么?
猜你喜欢
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多