|
-
Oct 25th, 2002, 03:57 PM
#1
Thread Starter
yay gay
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
-
Oct 25th, 2002, 04:17 PM
#2
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:
WebClient wcClient = new WebClient();
byte[] mBuffer = [b](byte[])[/b] wcClient.DownloadData("http://www.vbforums.com/images/ads/current.gif");
-
Oct 25th, 2002, 04:33 PM
#3
Thread Starter
yay gay
what exactly did that do?
-
Oct 25th, 2002, 04:35 PM
#4
Thread Starter
yay gay
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?
-
Oct 25th, 2002, 09:04 PM
#5
PowerPoster
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.
-
Oct 25th, 2002, 09:26 PM
#6
PowerPoster
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.
-
Oct 26th, 2002, 05:33 AM
#7
Thread Starter
yay gay
-
Oct 26th, 2002, 05:38 AM
#8
Thread Starter
yay gay
ah tks worked like a sharm...mmm..but what is a memorystream? lol
-
Oct 26th, 2002, 05:39 AM
#9
Thread Starter
yay gay
btw where are tuturials about casting..? that seems interesting lol
-
Oct 26th, 2002, 10:50 AM
#10
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|