Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Asp内核中将文件转换为图像_C#_Asp.net Core - Fatal编程技术网

C# 在Asp内核中将文件转换为图像

C# 在Asp内核中将文件转换为图像,c#,asp.net-core,C#,Asp.net Core,我需要调整文件上传,如果文件是图像 我写了以下扩展: public static Image ResizeImage(this Image image, int width, int height) { var res = new Bitmap(width, height); using (var graphic = Graphics.FromImage(res)) { graphic.InterpolationM

我需要调整文件上传,如果文件是图像

我写了以下扩展:

 public static Image ResizeImage(this Image image, int width, int height)
    {
        var res = new Bitmap(width, height);
        using (var graphic = Graphics.FromImage(res))
        {
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphic.CompositingQuality = CompositingQuality.HighQuality;
            graphic.DrawImage(image, 0, 0, width, height);
        }
        return res;
    }
这是
上传操作:

 [HttpPost("UploadNewsPic"), DisableRequestSizeLimit]
    public IActionResult UploadNewsPic(IFormFile file)
    {
        if (file.IsImage())
        {

        }
        try
        {
            if (file.Length > 0)
            {
                string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                string fullPath = Path.Combine(_applicationRoot.UploadNewPath(), file.Name);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
            }
            return Ok();
        }
        catch (Exception e)
        {
            return BadRequest();
        }
    }

现在我的问题是这里=>我的扩展只处理
图像
文件的类型,但这个文件的类型是
格式文件
。如何将
格式文件
转换为
图像
类型?

您需要使用OpenReadStream打开文件并转换为图像格式。并将其传递给扩展方法

FileDetails fileDetails;
                using (var reader = new StreamReader(file.OpenReadStream()))
                {
                   var fileContent = reader.ReadToEnd();
                   var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
                   fileDetails = new FileDetails
                      {
                          Filename = parsedContentDisposition.FileName,
                          Content = fileContent,
                          ContentType=file.ContentType
                       };
                 }

您应该使用
Image.FromStream()
方法将流作为图像读取:

public async Task<IActionResult> FileUpload(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest();
            }

            using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream);
                using (var img = Image.FromStream(memoryStream))
                {
                  // TODO: ResizeImage(img, 100, 100);
                }
            }
        }
公共异步任务文件上载(IFormFile)
{
if(file==null | | file.Length==0)
{
返回请求();
}
使用(var memoryStream=new memoryStream())
{
等待文件.CopyToAsync(memoryStream);
使用(var img=Image.FromStream(memoryStream))
{
//TODO:调整图像大小(img,100100);
}
}
}

您可以将文件放入字节/内存流中,然后从中创建BMP。@JohnB
System.Drawing
存在于.NET Core中。它是为2.1移植的,从那时起就可以使用。
FileDetails
是什么?创建一个自定义类,您可以添加到ViewModel中以保留文件数据。