Results 1 to 10 of 10

Thread: converting types...

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    converting types...

    im trying to download an image and show in a picturebox control...

    i've already got so far:

    PHP Code:
                WebClient wcClient = new WebClient();
                
    byte[] mBuffer wcClient.DownloadData("http://www.vbforums.com/images/ads/current.gif"); 
    but now i realised that in vb.net we have a function called CType() that isnt avaliable in C#..how do i convert that byte[]array into a bitmap image so i can show it in my picturebox without having to write it to the disk? lol

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You use casting, I believe there is more than one way to do it but one way is to put the target type in parenthesis in front of the object you want to convert.

    VB Code:
    1. WebClient wcClient = new WebClient();
    2.             byte[] mBuffer = [b](byte[])[/b] wcClient.DownloadData("http://www.vbforums.com/images/ads/current.gif");

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    what exactly did that do?

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i think the best way is to maybe convert to a stream and then use the GetStream or something like that that the Image Type supports lol or what?

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    what exactly did that do?
    He is casting the return value that is returned from the downloadData method. Casting means converting one data type to another compatable data type. For instance:

    Code:
        float myFloat = 145.654f
        int myInt = (int) myFloat;
    Without explicity casting myFloat to myInt, the compiler would puke all over this. Reason being, we are trying to stuff a larger data type into a smaller type, which is known as a narrowing conversion (potential loss of data). When you explicity cast a data type this way, you are treating the data type as another data type.

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here ya go, you can add in your own exception handling if you would like:

    Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace ImageStream
    {
    	public class ImageToDownload
    	{
    		string urlToDownload;
    		const string prefix = "http://";
    
    		public ImageToDownload(string uRL)
    		{
    			this.urlToDownload = uRL;
    		}
    
    		public MemoryStream BeginDownload()
    		{
    			MemoryStream msImage;
    			WebClient wcClient = new WebClient();
    
    			if  (!urlToDownload.ToLower().StartsWith("http://"))
    			{
    				urlToDownload = prefix + urlToDownload;
    			}
    
    			msImage = new MemoryStream(wcClient.DownloadData (urlToDownload));
    			return msImage;					
    		}
    	}
    }

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {			
    	string pathToImage = "http://www.vbforums.com/images/ads/current.gif";
    	ImageToDownload myImage = new ImageToDownload(pathToImage);			
    	MemoryStream stream = myImage.BeginDownload();
    	pictureBox1.Image = Image.FromStream(stream);
    }
    Last edited by Lethal; Oct 25th, 2002 at 10:53 PM.

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm let me try lol

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah tks worked like a sharm...mmm..but what is a memorystream? lol

  9. #9

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    btw where are tuturials about casting..? that seems interesting lol

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty.

    Do a search on casting, and I'm sure you'll find many examples.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width