![](/img/trans.png)
[英]How to convert the ordinary jpeg image bytes to the DICOM native image bytes using C#
[英]how to convert image into bytes [] in c#
這將是一個菜鳥問題。 我是C#的新手
我有一個用於將圖像保存到服務器的文件上傳服務器控件。
我的庫中有一個帶有此參數的圖像調整大小函數:
byte[] resizeImage(byte[] image, int witdh)
我應該使用的。 現在,我將文件保存為某些路徑,如upImage.saveas(“ filepath”);。
好?
現在,我想調整此圖像的大小,我無法弄清楚如何將該圖像轉換為字節???
文章在哪里轉換? 我看到的每個地方,我只能看到要成像的字節,但我想以其他方式處理。
請幫忙?
您的意思是將保存的文件轉換為byte[]
數組嗎? 如果是這樣,您可以使用File.ReadAllBytes
:
byte[] imageBytes = File.ReadAllBytes("example.jpg");
如果要直接從FileUpload
控件中獲取byte[]
數組,則可以使用FileBytes
屬性:
byte[] imageBytes = yourUploadControl.FileBytes;
看起來您可以將其保存到MemoryStream
然后將MemoryStream
轉換為字節數組。 從這里
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray
}
在post方法中將文件轉換為fileuploadcontrol中的字節
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);
}
}
}
好吧,簡短的答案是
File.ReadAllBytes("filepath");
但是長的答案是,聽起來好像您不知道發生了什么,所以我建議您確保以您認為的方式對圖像進行實際編碼(實際上是磁盤上的位圖,還是它是位圖?壓縮嗎? resizeImage
實際上對字節有什么作用,它期望什么樣的圖像?為什么resizeImage
不采用Image
而是僅用GDI +調整大小?)否則,您可能會感到驚訝。
您可以使用Image.Save函數來傳遞內存流。 完成后,可以使用MemoryStream.Read或MemoryStream.ToArray恢復字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.