TypeConvertion Get image from Url Async

1 Answer 38 Views
GridView
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.

Kapil
Top achievements
Rank 1
Iron
commented on 10 Aug 2021, 08:09 AM

Hii I m facing an issue in loading a theme package 

ThemeResolutionService.LoadPackageResource("FMS.UI.FMS2021Theme.tssp")

i tried puting FMS2021Thema in main directory, in resource directly, in folder everyWhere but it still throw error

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 11 Aug 2021, 08:50 AM

Hello, Kapil,

If you are using the ThemeResolutionService.LoadPackageResource method, please have in mind that this method loads a theme package file that is contained in the project as an EmbeddedResource. This is a convenient method for loading a theme package since the resource path to the package is not changed when the application is deployed. The path construction is DefaultProjectNamespace.ThemeFolder.ThemePackageFile. The ThemeFolder part should only be used if the package is contained in a folder under the main project directory and if the project programming language is C#. In VB.NET project you do not need to include ThemeFolder part even if the package file is contained in a folder.

An alternative approach is to use the LoadPackageFile method - loads a file from a specified directory on the system. Depending on how the directory is defined (full or relative), the path to the package may change when the application is deployed on another machine. 

ThemeResolutionService.LoadPackageFile(@"C:\CustomTheme.tssp");

In case you are still experiencing any further difficulties, feel free to submit a support ticket from your Telerik account where you can provide a sample project demonstrating the problem you are facing. Thus, we would be able to make an adequate analysis of the precise case and provide further assistance.

I hope this information helps.

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