简体   繁体   中英

How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c#

Basically what I am trying to do is sending a string which was bitmap(I converted into string ) and send t to Asp.net core 1.0 web application/api. I want to save that image into the hosted application files and path of that image to database but I do not understand how can I convert that string into IFormFile so I can copy It to the specific path.

        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.
        }

I have tried converting it to Byte array but don't know what to do with that.

             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);

This is the code in android studio to get the file and convert it into string and pass that file

Bitmap

A bitmap is a type of memory organization or image file format used to store digital images. The term bitmap comes from the computer programming terminology, meaning just a map of bits , a spatially mapped array of bits. Now, along with pixmap, it commonly refers to the similar concept of a spatially mapped array of pixels.
It's "raw" image files, which store raw bitmaps with no header , size or other information.

IFormFile

Meanwhile, IFormFile has the following structure:

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);
}

From here, you can see the difference between String ( =Bitmap ) and IFormFile . So that won't convert success as you expect.

Convert base64 string to 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"
            };
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM