TypeConvertion Get image from Url Async
1 Answer 11 Views
Kapil
Top achievements
Rank 1
Iron
Kapil asked on 29 May 2021, 12:52 PM
I have this code and i want to load convert to method to load image async Is there any way to do that
 public class MyTypeConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(Image)))
                return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
            System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType.Equals(typeof(Image)))
            {
                WebRequest request = System.Net.WebRequest.Create(value + "");

                using (var response = request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        return Bitmap.FromStream(stream);
                    }
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

1 Answer, 1 is accepted

Sort by
0
Nadya
Telerik team
answered on 01 Jun 2021, 02:57 PM

Hello, Kapil,

I have noticed that you posted the same question in another forum post. Please see our answer there for more information. We kindly ask you to use just one thread for a specific problem to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

For your reference, I created an example using HttpWebRequest which response is expected to return an image that you can show in the grid:

public class SeverityImageConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(Image))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value == null)
            throw new ArgumentNullException("value");

        if (value is string)
        {

            string url = value.ToString();
            HttpWebRequest request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Image image = Image.FromStream(response.GetResponseStream());
            response.Close();

            return image;

        }
        else
            return base.ConvertTo(context, culture, value, destinationType);
    }

I hope this helps. Should you have other questions please let me know.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Asked by
Kapil
Top achievements
Rank 1
Iron
Answers by
Nadya
Telerik team
Share this question
or