Reputation: 735
I need to pass an image value to a function, but I don't know which data type should I use? I really appreciate your answers...
The typical function look like this:
void UserInfo(string userName, string userEmail,((image type? )) userImagel)
{
// code here
}
I am using Twitter API, after a successful logged in. I want to save the users info in a database using a function. SO they can import their profile information from twitter to our website.
in Default.aspx there is a line of code like this:
<img src="<%=profileImage%>" />
in Default.aspx.cs I used
public string profileImage="";
.
.
.
profileImage = Convert.ToString(o["profile_image_url"]);
So by using that way the profile picture appears on the web page. it's obvious that it comes as a link (URL) . Now, how can I save that image from that URL in my database? and how to pass its value in a function?
Best Regards
Upvotes: 1
Views: 8503
Reputation: 9323
Generally, I would avoid storing an image binary in a database unless there is a particular reason to do so. In any case, as you describe it, you do not yet have an image, you have the image's URL:
private void UserInfo(string userName, string userEmail, string imageURL);
The question the, is what to do with the image.
If you want to obtain the actual image, you can download it with the System.Net.WebClient
, for example:
private void UserInfo(string userName, string userEmail, string imageURL)
{
WebClient client = new WebClient();
byte[] imgData = client.DownloadData(imageURL);
// store imgData in database (code depends on what API you're
// using to access the DB
}
There are more likely and scalable scenarios, though:
You could store the image's URL in your database and use that in your web pages, letting Twitter serve the images (save you bandwidth).
You could download the image (as above) and then store it on your web server's hard disk instead of the database. That way image requests can be handled by the web server instead of the database, which generally has quite a few advantages if your service grows (caching, CDN, ...)
Upvotes: 2
Reputation: 12459
Create an object of your image file:
Bitmap bimage = new Bitmap(@"C:\Pictures\2765.jpg");
and pass this object through your function:
UserInfo("abc", "[email protected]", bimage);
To receive image:
void UserInfo(string userName, string userEmail, Bitmap userImagel)
{
// code here
}
Upvotes: 1
Reputation: 9583
How about:
void UserInfo(string userName, string userEmail, System.Drawing.Image userImagel)
{
// code here
}
Upvotes: 2