473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to save image file from stream?

17
Expand|Select|Wrap|Line Numbers
  1.  
  2. Stream st;
  3. FileStream f = new FileStream(@"D:\samp\rt.jpg", FileMode.Create);
  4. byte[] b = new byte[10000];
  5.  
  6. st.Read(b, 0, b.Length);
  7.  
  8. st.Close();
  9. f.Write(b, 0, b.Length);
  10. f.Flush();
  11. f.Close();
  12.  
i doesn't show error.
but it gets noting stored.why?
but other files would be saved.but incase of image file,its happening thus.
Apr 28 '09 #1
12 41099
wastman
17
Expand|Select|Wrap|Line Numbers
  1. Stream st=fromsomesource;
  2.  FileStream f = new FileStream(@"D:\samp\rt.jpg", FileMode.Create);
  3. byte[] b = new byte[10000];
  4. st.Read(b, 0, b.Length);  
  5. st.Close();
  6. f.Write(b, 0, b.Length);
  7. f.Flush();
  8. f.Close();
i doesn't show error.
but it gets noting stored.why?
but other files would be saved.but incase of image file,its happening thus.
Apr 28 '09 #2
PRR
750 Expert 512MB
FileStream Read
Expand|Select|Wrap|Line Numbers
  1. //Read image file 
  2. byte[] b = null;
  3.             using (FileStream f = new FileStream(@"C:\Bernese Oberland.jpg", FileMode.Open))
  4.             {
  5.                 b = new byte[f.Length];
  6.  
  7.                 f.Read(b, 0, b.Length);
  8.             }
  9.  
  10. // Write to file ...
  11. using (FileStream fs = new FileStream(@"C:\Bernese Oberland1.jpg", FileMode.Create))
  12.             {
  13.                 fs.Write(b, 0, b.Length);
  14.  
  15.             }
  16.  
Apr 28 '09 #3
wastman
17
No. stream is coming from webresponse.getStream() method.
so i need a solution for that one.
Apr 28 '09 #4
Curtis Rutland
3,256 Expert 2GB
Well, there's probably an easier way, but if you wanted you could use the ReadByte method of the stream and add each byte you read to a System.Collectiosn.Generic.List<byte> until the byte you read is -1 (end of stream) and then convert the List into a byte[].

Lots of ways to do it though.
Apr 28 '09 #5
wastman
17
@insertAlias
but image does not get stored.should i follow something like sequential access?
Apr 29 '09 #6
PRR
750 Expert 512MB
Could you post more sample code? and maybe flow of your program...
Apr 29 '09 #7
wastman
17
@DeepBlue
Expand|Select|Wrap|Line Numbers
  1.         ArrayList addrs = new ArrayList();
  2.         //addrs.Add("http://iis02.northcomp.com");
  3.         //addrs.Add("http://iis03.northcomp.com");
  4.         //addrs.Add("http://ncs01.northcomp.com");
  5.         addrs.Add("http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif");
  6.         foreach (string s in addrs)
  7.         {
  8.             try
  9.             {
  10.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
  11.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  12.                 //HttpResponse rd = new HttpResponse();
  13.  
  14.                 Stream st = resp.GetResponseStream();
  15.                 FileStream f = new FileStream(@"f:\pic\samp\rt.jpg", FileMode.Create);
  16.                 byte[] b = new byte[10000];
  17.  
  18.                 st.Read(b, 0, b.Length);
  19.  
  20.                 st.Close();
  21.                 f.Write(b, 0, b.Length);
  22.                 f.Flush();
  23.                 f.Close();
  24.                 //StreamReader sr = new StreamReader(st);
  25.  
  26.                 //string buffer = sr.ReadToEnd();
  27.                 //int startPos, endPos;
  28.                 //startPos = buffer.IndexOf("&lt;title>",
  29.                 //   StringComparison.CurrentCultureIgnoreCase) + 7;
  30.                 //endPos = buffer.IndexOf("&lt;/title>",
  31.                 //   StringComparison.CurrentCultureIgnoreCase);
  32.                 //string title = buffer.Substring(startPos, endPos - startPos);
  33.  
  34.                 //Console.WriteLine("Response code from {0}: {1}", s,
  35.                 //                  resp.StatusCode);
  36.                 //Console.WriteLine("Page title: {0}", title);
  37.                 //sr.Close();
  38.                 //st.Close();
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.                 System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
  43.             }
  44.         }
  45.         System.Windows.Forms.MessageBox.Show("Web site check completed");
  46.  
  47.  
Apr 29 '09 #8
wastman
17
Give me a solution,i don't want to waste my time though my name is wastman.
Apr 29 '09 #9
wastman
17
Expand|Select|Wrap|Line Numbers
  1. string s="http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif";
  2.         try
  3.             {
  4.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
  5.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  6.                 Stream st = resp.GetResponseStream();
  7.                 FileStream f = new FileStream(@"f:\pic\samp\rt.jpg", FileMode.Create);
  8.  
  9.                 byte[] b = new byte[10000];
  10.                 st.Read(b, 0, b.Length);
  11.                 st.Close();
  12.  
  13.                 f.Write(b, 0, b.Length);
  14.                 f.Flush();
  15.                 f.Close();
  16.             }
  17.             catch (Exception ex)
  18.             {
  19.                 System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
  20.             }
  21.  
  22.         System.Windows.Forms.MessageBox.Show("Web site check completed");
  23.  
  24.  
Please get me out of a minor mistake which might be.
Apr 29 '09 #10
PRR
750 Expert 512MB
Try this
Expand|Select|Wrap|Line Numbers
  1. public static void Image()
  2.         {
  3.             ArrayList addrs = new ArrayList();
  4.  
  5.  
  6.             //addrs.Add("http://iis02.northcomp.com");
  7.             //addrs.Add("http://iis03.northcomp.com");
  8.             //addrs.Add("http://ncs01.northcomp.com");
  9.             addrs.Add("http://images.devshed.com/af/stories/C_Sharp_Filestream/4.jpg");//http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif");
  10.             foreach (string s in addrs)
  11.             {
  12.                 try
  13.                 {                    
  14.                     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
  15.                    //req.ContentType = "image/gif";
  16.  
  17.  
  18.                     HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  19.                     long l = resp.ContentLength;
  20.                     byte[] b = new byte[l]; 
  21.  
  22.  
  23.                     using (Stream st = resp.GetResponseStream())
  24.                     {                     
  25.                             using (FileStream f = new FileStream(@"C:\one.jpg", FileMode.OpenOrCreate))
  26.                             {
  27.                                 //long len = resp.ContentLength;
  28.  
  29.  
  30.                                 st.Read(b, 0, b.Length);
  31.  
  32.                                 st.Close();
  33.                                 f.Write(b, 0, b.Length);
  34.                                 f.Flush();
  35.                                 f.Close();
  36.                             }
  37.  
  38.                     }
  39.                     //StreamReader sr = new StreamReader(st);
  40.  
  41.                     //string buffer = sr.ReadToEnd();
  42.                     //int startPos, endPos;
  43.                     //startPos = buffer.IndexOf("&lt;title>",
  44.                     //   StringComparison.CurrentCultureIgnoreCase) + 7;
  45.                     //endPos = buffer.IndexOf("&lt;/title>",
  46.                     //   StringComparison.CurrentCultureIgnoreCase);
  47.                     //string title = buffer.Substring(startPos, endPos - startPos);
  48.  
  49.                     //Console.WriteLine("Response code from {0}: {1}", s,
  50.                     //                  resp.StatusCode);
  51.                     //Console.WriteLine("Page title: {0}", title);
  52.                     //sr.Close();
  53.                     //st.Close();
  54.                 }
  55.                 catch (Exception ex)
  56.                 {
  57.                     System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
  58.                 }
  59.             }
  60.             System.Windows.Forms.MessageBox.Show("Web site check completed");
  61.  
  62.  
  63.         }
  64.         #endregion
  65.  
Apr 29 '09 #11
wastman
17
No.
The Actual Image:



but the output image:



so that i have got to guess if i have to check some thing like sequential access.
Please help me.
Attached Images
File Type: jpg one.jpg (13.4 KB, 8836 views)
Apr 30 '09 #12
PRR
750 Expert 512MB
use this function
Expand|Select|Wrap|Line Numbers
  1. ArrayList addrs = new ArrayList();
  2.  
  3. addrs.Add("http://www.charlespetzold.com/key/keycs.jpg");
  4.             addrs.Add("http://www.nbcindia.com/Booksimages/1931841306.gif");
  5.  
  6.  
  7. int j = 0;
  8.             foreach (string s in addrs)
  9.             {
  10.                 try
  11.                 {                    
  12.                     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
  13. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  14.  
  15. using (Stream st = resp.GetResponseStream())
  16.                     {
  17.                         MemoryStream ms =(MemoryStream) CopyStream(st);
  18.                         long len = ms.Length;
  19.                         byte[] b = new byte[len];
  20.  
  21.                             //using (FileStream f = new FileStream(@"C:\one"+j+".jpg", //FileMode.OpenOrCreate))
  22.                             {
  23.                                 ++j;
  24.                                 //long len = resp.ContentLength;
  25.  
  26.                                 using (Image image = Image.FromStream(ms))
  27.                                 {
  28.                                     //st.Read(b, 0, b.Length);
  29.  
  30.                                     image.Save(@"C:\one" + j + ".jpg");
  31.                                 }
  32. }
  33. }
  34.  
  35.  
Expand|Select|Wrap|Line Numbers
  1. // Author Jon Skeet
  2. private static Stream CopyStream(Stream inputStream)
  3.         {
  4.             const int readSize = 256;
  5.             byte[] buffer = new byte[readSize];
  6.             MemoryStream ms = new MemoryStream();
  7.             int count = inputStream.Read(buffer, 0, readSize);
  8.             while (count > 0)
  9.             {
  10.                 ms.Write(buffer, 0, count);
  11.                 count = inputStream.Read(buffer, 0, readSize);
  12.             }
  13.             ms.Seek(0, SeekOrigin.Begin);
  14.             return ms;
  15.         }
  16.  
I tried with 5-6 images ... almost all are "completely" saved ....
May 6 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Kevin | last post by:
Hi all, Assuming I have stored the binary data of an image file in a blob field of a table, how can I display it as an image in a web page? I found some instructions on the web, such as ...
0
by: Umesh | last post by:
Hi, I have an Application, in which 1) need to post data to a URL(Remote Server), by using HTTPRequest. 2) get the Image data in the form of Stream in Response. 3) need to save this stream as a...
1
by: andrewcw | last post by:
Missing the obvious here..thanks.. I need to open the file exclusively for on entry like this: FileStream stream = null; stream = File.Open(fiPath, FileMode.Open,...
0
by: a | last post by:
Save text file as html kloepper 17:42 23 Jul '04 I'm using httpwebresponse and a StringBuilder to return a stream that originates as a file with the .txt suffix (My download code converts the html...
4
by: David W. Simmonds | last post by:
Is there a way I can have a button on a ASP.NET form that when clicked will allow the user to save the image to a file on the client side? I know that the user can simply rclick the image and...
4
by: moondaddy | last post by:
Using vb.net I need to download image files to the client browser where they can save to disk. Below is some sample code I'm using. when I run this the File Download window in the browser says: ...
1
by: http://www.visual-basic-data-mining.net/forum | last post by:
I have a page where it requires user to upload an image file to the server. For the uploading, i am using HTML imput file control. Is there a way where after the user has selected a picture file,...
0
by: hnpatel | last post by:
hello friends, i m making application in vb.NET. i m using MY SQL 5.0 and vs 2005 for this application. Now I want to save image file in sql database. i m using blob datatype of image field. i...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.