简体   繁体   English

C# 图片上传保存后变黑 - ASP.NET MVC 5

[英]C# Picture become black after uploading and then saving it - ASP.NET MVC 5

Please bear with me, my English is still improving... Below given code is what I am using to get an uploaded image saved in "UploadedImage" folder in ASP.NET MVC 5 project.请耐心等待,我的英语仍在进步中……下面给出的代码是我用来获取保存在 ASP.NET MVC 5 项目的“UploadedImage”文件夹中的上传图像的代码。 But when I use my almost high-end PC having i7 processor, 16GB of RAM, 4GB Graphics Card, all works like a charm.但是,当我使用配备 i7 处理器、16GB RAM、4GB 显卡的近乎高端的 PC 时,一切都运行良好。 But problem arises when I use my laptop having i3 processor and 8GB of ram or a PC having i3 processor having 4GB RAM.... What I get is only a black image.但是,当我使用配备 i3 处理器和 8GB 内存的笔记本电脑或配备 i3 处理器和 4GB 内存的 PC 时,问题就出现了……我得到的只是一张黑色图像。

public ActionResult Index(FormCollection form)
{ if (string.IsNullOrEmpty(form["image-data"])) return
    View();
var t = form["image-data"].Substring(form["image-data"].LastIndexOf(',') + 1);
byte[] bytes = Convert.FromBase64String(t);
string fileName = form["email"].Replace("@", "at");
fileName = fileName.Replace(fileName[fileName.LastIndexOf(".")].ToString(), "dot");
var fullPath = Path.Combine(Server.MapPath("~/UploadedImage/"), fileName + ".png");

Image image;

using (var streamBitmap = new MemoryStream(bytes))
{
    using (image = Image.FromStream(streamBitmap))
    {
        image.Save(fullPath);
        }
    }

Please help me, I could not get any help from anywhere, thanks in advance请帮助我,我无法从任何地方得到任何帮助,在此先感谢

You can try this method to upload file/images您可以尝试使用此方法上传文件/图片

var Upload_Path = "get file path";
string fileExtension = Path.GetExtension(file);
fileExtension = fileExtension.ToLower();
var path = System.IO.Path.Combine(Server.MapPath(Upload_Path),"name.jpg");
Model.ProfileImage.SaveAs(path);

in model you can use在 model 你可以使用

[DisplayName("Picture")]
public HttpPostedFileBase ProfileImage { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# & ASP.NET MVC 中上传大文件 - Uploading Large File in C# & ASP.NET MVC 通过C#将图片上传到ASP.NET MVC 3操作 - Uploading Pictures via C# to ASP.NET MVC 3 Action C#ASP.NET MVC代码首先不保存更改 - C# ASP.NET MVC Code First Not Saving Changes 使用asp.net C#上载Excel工作表时未保存数据 - Data is not saving while excel sheet uploading using asp.net C# 在表中保存记录后,如何循环遍历具有相同 ID 的所有记录并在 ASP.NET MVC C# 中更新它们 - After saving a record in a table, how to loop through all records with same ID and update them in ASP.NET MVC C# 如何在Asp.net MVC Web应用程序c#中上传文件时显示进度? - How to show the progress while uploading a file in Asp.net MVC web application, c#? MVC ASP.NET CORE,在上传的文件被破坏/大小为0字节时上传文件 - MVC ASP.NET CORE, Uploading files sometime the file being uploaded become corrupted / size 0 byte 在ASP.NET Core MVC中上传文件后立即读取文件 - Read a file immediately after uploading it in ASP.NET core MVC 在 ASP.net MVC 3 C# 中保存来自 HTML 表单的图像 - Saving an image from HTML form in ASP.net MVC 3 C# 通过AJAX MVC下载Excel文件,而无需将文件保存在ASP.Net/C#中的服务器上 - Download Excel file via AJAX MVC without saving the file on server in ASP.Net/C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM