Click here to Skip to main content
14,391,651 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,

Actually my application i have the upload image functionality there i have passed one parameter(base64string) to my controller. here i want to check the base64string type . i tried one way but that was wrong if anybodys know please tell me that answer .


i need to change this line . how to check the type inside that string ,

string x = base64image.Replace("data:image/png|tiff|jpg|gif;base64,", "");


What I have tried:

public bool LoadImage([FromBody]string base64image)
      {
          string x = base64image.Replace("data:image/png|tiff|jpg|gif;base64,", "");
          byte[] data = System.Convert.FromBase64String(x);
          MemoryStream ms = new MemoryStream(data);
          Image img = Image.FromStream(ms);
          return false;
      }
Posted
Updated 17-Jul-19 0:20am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use a Regular Expression:
using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;

public sealed class DataImage
{
    private static readonly Regex DataUriPattern = new Regex(@"^data\:(?<type>image\/(png|tiff|jpg|gif));base64,(?<data>[A-Z0-9\+\/\=]+)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
    
    private DataImage(string mimeType, byte[] rawData)
    {
        MimeType = mimeType;
        RawData = rawData;
    }
    
    public string MimeType { get; }
    public byte[] RawData { get; }
    
    public Image Image => Image.FromStream(new MemoryStream(RawData));
    
    public static DataImage TryParse(string dataUri)
    {
        if (string.IsNullOrWhiteSpace(dataUri)) return null;
        
        Match match = DataUriPattern.Match(dataUri);
        if (!match.Success) return null;
        
        string mimeType = match.Groups["type"].Value;
        string base64Data = match.Groups["data"].Value;
        
        try
        {
            byte[] rawData = Convert.FromBase64String(base64Data);
            return rawData.Length == 0 ? null : new DataImage(mimeType, rawData);
        }
        catch (FormatException)
        {
            return null;
        }
    }
}

public bool LoadImage([FromBody] string base64image)
{
    DataImage image = DataImage.TryParse(base64image);
    if (image == null) return false;
    
    Image img = image.Image;
    // Do something with the image...
    return true;
}

Regular Expression Language - Quick Reference[^]
   
Comments
Member 11183217 5-Jun-17 8:01am
   
Thank you so much Richard.i got d actual functionality because of yur idea
Rate this:
Please Sign up or sign in to vote.

Solution 2

Changed Richard code. TryParse return bool and added imageType, because I dont see usage for mimeType

public sealed class DataImage
    {
        private static readonly Regex DataUriPattern = new Regex(@"^data\:(?<mimeType>image\/(?<imageType>png|tiff|jpg|gif));base64,(?<data>[A-Z0-9\+\/\=]+)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

        private DataImage(string mimeType, string imageType, byte[] rawData)
        {
            MimeType = mimeType;
            RawData = rawData;
            ImageType = imageType;
        }

        public string MimeType { get; }
        public byte[] RawData { get; }
        public string ImageType { get; }

        public Image Image => Image.FromStream(new MemoryStream(RawData));

        public static bool TryParse(string dataUri, out DataImage image)
        {
            image = null;
            if (string.IsNullOrWhiteSpace(dataUri)) return false;

            Match match = DataUriPattern.Match(dataUri);
            if (!match.Success) return false;

            string mimeType = match.Groups["mimeType"].Value;
            string imageType = match.Groups["imageType"].Value;
            string base64Data = match.Groups["data"].Value;

            try
            {
                byte[] rawData = Convert.FromBase64String(base64Data);
                image = rawData.Length == 0 ? null : new DataImage(imageType, rawData);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }


usage:

if (DataImage.TryParse(propertyValue, out var imageData))
{
    picture = imageData.RawData;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100