C# 如何在c中将图像转换为字节[]#

C# 如何在c中将图像转换为字节[]#,c#,C#,这将是一个noob问题。 我是c的新手# 我有一个文件上传服务器控件,用于将图像保存到服务器 在我的库中,我有一个带有以下参数的图像大小调整函数: byte[] resizeImage(byte[] image, int witdh) 我应该用的。 现在,我正在将我的文件保存到某个路径,作为upImage.saveas(“filepath”) 好吗 现在,我想调整这个图像的大小,但我不知道如何将该图像转换为字节 要转换的物品在哪里?在我看到的任何地方,我只能看到图像中的字节,但我想换一种方式

这将是一个noob问题。 我是c的新手#

我有一个文件上传服务器控件,用于将图像保存到服务器

在我的库中,我有一个带有以下参数的图像大小调整函数:

byte[] resizeImage(byte[] image, int witdh)
我应该用的。 现在,我正在将我的文件保存到某个路径,作为upImage.saveas(“filepath”)

好吗

现在,我想调整这个图像的大小,但我不知道如何将该图像转换为字节

要转换的物品在哪里?在我看到的任何地方,我只能看到图像中的字节,但我想换一种方式


请帮助?

您的意思是将保存的文件转换为
字节[]
数组吗?如果是,您可以使用:

如果要直接从控件获取
字节[]
数组,则可以使用以下属性:

byte[] imageBytes = yourUploadControl.FileBytes;

看起来您可以将其保存到
内存流
,然后将
内存流
转换为字节数组。从


简而言之,答案是

File.ReadAllBytes(“文件路径”)


但长话短说的答案是,听起来你根本不知道发生了什么,所以我建议你确保图像是按照你认为的方式编码的(它实际上是磁盘上的位图,还是经过压缩的?
resizeImage
实际上对字节有什么作用?它希望得到什么类型的图像?为什么不
resizeImage
拍摄一幅
图像
并用GDI+调整其大小?)否则您可能会大吃一惊。

您可以使用Image.Save函数,传入内存流。一旦完成,您可以使用MemoryStream.Read或MemoryStream.ToArray来恢复字节。

在fileuploadcontrol on post方法中将文件转换为字节

public ActionResult ServiceCenterPost(ServiceCenterPost model)
{
     foreach (var file in model.FileUpload)
     {
          if (file != null && file.ContentLength > 0)
          {
             byte[] fileBytes = new byte[file.ContentLength];
             file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
             string thumbpath = _fileStorageService.GetPath(fileName);
            _fileStorageService.SaveFile(thumbpath, fileBytes);
         }
     }
}

否属于上载控制类型。当客户端单击“浏览”按钮选择其“我的图片”中的图像,然后将其保存。如果是客户端,则无法运行C#codehehehe好笑话,当客户端单击“保存”时,页面在服务器中执行否?=kierenjohnstone@carlos当前位置我不知道。它们通常是解决细微不同问题的细微不同的方法因为我不确定你到底有什么问题,所以我提出了几个建议,希望其中一个能符合你的具体要求。
MemoryStream
Yeeeaaaaaauhhhh
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray
}
public ActionResult ServiceCenterPost(ServiceCenterPost model)
{
     foreach (var file in model.FileUpload)
     {
          if (file != null && file.ContentLength > 0)
          {
             byte[] fileBytes = new byte[file.ContentLength];
             file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
             string thumbpath = _fileStorageService.GetPath(fileName);
            _fileStorageService.SaveFile(thumbpath, fileBytes);
         }
     }
}