C#关于图像转换为字节数组 [英] c# regarding to image convert to byte array

查看:107
本文介绍了C#关于图像转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看我的问题.



我有100张图片.

我想通过Windows Mobile应用程序通过Web服务发送图像.



以前,我曾经将文件流转换为字节数组.

但是这会花费太多时间,并且每次我收到套接字异常时.

所以

我正在尝试快速发送.所以我选择了内存流
.

look at my problem.



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
.

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

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







您能否建议哪种流更适合发送大量图像?

如何避免该套接字异常?



nd







could you please suggest which stream is better to send lot of images?

how to avoid that socket exception?



please any one suggest me regarding this problems?nd

推荐答案

我认为您需要学习一些基础知识:
1)要将我的图像发送到上述功能",请以您的图像作为参数调用该方法:
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:
byte[] data = ConvertImagetoByteArray(myImage);


2)该方法不返回流:它使用流将图像转换为字节数组.然后,它返回字节数组.
3)在不知道您在使用套接字做什么以及异常是什么的情况下,我们无法确定您的错误是什么.很可能是超时,但是没有相关的代码片段和错误,这只是猜测...


我收到此错误:无法从"字符串"转换为" System.Drawing.Image"
我的代码是:"


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:"

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 ); 
}


我正在这样做,但是我遇到了上面提到的错误"

这里有几件事:
1)将字符串强制转换为图像不会加载文件.
2)如果要处理文件名,则根本不需要将图像作为图像加载.
请替换演员表:


"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:

Image image = (Image) imagename;

(带有图片加载):

with an Image load:

Image image = Image.FromFile(imagename);


或者根本不将其作为图像加载;直接将其作为字节加载:


Or do not load it as an image at all; load it directly as bytes:

string[] images = Directory.GetFiles(subDirectory);
foreach (string filename in images)
   {
   byte[] imageByteArray = File.ReadAllBytes(filename);
   }


您可以像以下代码一样将图像转换为字节,而不是上载器,您可以选择图像来源.

you can convert image in to byte like this code inplace of uploader you can choose your source of image.

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变量包含字节形式的图像,您可以使用它发送.



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


这篇关于C#关于图像转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆