Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a 100 images. I want to send images through web service from windows mobile application.

Previously I have used to convert from file stream to byte array, but it is taking too much time and every time I am getting socket Exception.

So, I am trying to send fast, so I select memory stream

C#
public byte[] ConvertImagetoByteArray(System.Drawing.Image imageIn)
{
   MemoryStream ms = new MemoryStream();
   imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
   return ms.ToArray();
}


In this how can I send my image to above function and one more thing I didn't get any options like below

C#
newImage = Image.FromFile("SampImag.jpg");


Could you please suggest which stream is better to send lot of images? How to avoid that socket exception?
Posted
Updated 20-Nov-23 13:55pm
v2

I think you need to look at learning some basics:
1) To "send my image to above function" you call the method with your image as a parameter:
C#
byte[] data = ConvertImagetoByteArray(myImage);

2) That method does not return a stream: it uses a stream to convert the image to an array of bytes. It then returns the byte array.
3) Without knowing what you are doing with the socket, and what the exception is, we cannot tell what your error is. Most likely, it is a timeout, but without the relevant code fragment and error, that is just guessing...


"i got this Error: cannot convert from 'string' to 'System.Drawing.Image'
my code is:"

C#
string[] images = Directory.GetFiles(subDirectory);
                    for (int k = 0; k < images.Length; k++)
                    {
                        
                        string imagename = Path.GetFileName(images[k]);
Image image = (Image)imagename ;
  Byte[] imageByteArray = ConvertImagetoByteArray(image ); 
}

"iam doing like this but i am getting above mentioned error"

There a couple of things here:
1) Casting a string to an Image does not load a file.
2) If you are handling file names, you do not need to load the image as an Image at all.
Either replace the cast:
C#
Image image = (Image) imagename;
with an Image load:
C#
Image image = Image.FromFile(imagename);

Or do not load it as an image at all; load it directly as bytes:
C#
string[] images = Directory.GetFiles(subDirectory);
foreach (string filename in images)
   {
   byte[] imageByteArray = File.ReadAllBytes(filename);
   }
 
Share this answer
 
v2
Comments
Mehdi Gholam 17-Sep-11 4:19am    
My 5!
venuchary 17-Sep-11 6:48am    
i got this Error: cannot convert from 'string' to 'System.Drawing.Image'
my code is:
string[] images = Directory.GetFiles(subDirectory);
for (int k = 0; k < images.Length; k++)
{

string imagename = Path.GetFileName(images[k]);
Image image = (Image)imagename ;
Byte[] imageByteArray = ConvertImagetoByteArray(image );
}

iam doing like this but i am getting above mentioned error
OriginalGriff 17-Sep-11 8:29am    
Answer updated
you can convert image in to byte like this code inplace of uploader you can choose your source of image.

C#
FileUpload img = (FileUpload)FileUploadEdit;
           Byte[] imgByte = null;
           if (img.HasFile && img.PostedFile != null)
           {
               //To create a PostedFile
               HttpPostedFile File = FileUploadEdit.PostedFile;
               //Create byte Array with file len
               imgByte = new Byte[File.ContentLength];
               //force the control to load data in array
               File.InputStream.Read(imgByte, 0, File.ContentLength);
           }


imgByte variable consist image in byte form you can use it to send .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900