I'm uploading an image to a varbinary column in the database successfully. But I want to resize it before uploading.
So this is what I tried:
Guests model:
public byte[] Image { get; set; }
public string ContentType { get; set; }
ImageExtentions Class to resize the image:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace System.Drawing
{
public static class ImageExtentions
{
public static Image Resize(this Image current, int maxWidth, int maxHeight)
{
int width, height;
if (current.Width > current.Height)
{
width = maxWidth;
height = Convert.ToInt32(current.Height * maxHeight / (double)current.Width);
}
else
{
width = Convert.ToInt32(current.Width * maxWidth / (double)current.Height);
height = maxHeight;
}
var canvas = new Bitmap(width, height);
using (var graphics = Graphics.FromImage(canvas))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(current, 0, 0, width, height);
}
return canvas;
}
public static byte[] ToByteArray(this Image current)
{
using (var stream = new MemoryStream())
{
current.Save(stream, current.RawFormat);
return stream.ToArray();
}
}
}
}
GuestsController:
//using a lot using System.Drawing; namespace Proj.Controllers { public class GuestsController : Controller { private readonly ApplicationDbContext _context;
public GuestsController(ApplicationDbContext context)
{
_context = context;
}
//Some code
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,DoB")] Guests guest, List<IFormFile> Image)
{
IFormFile uploadedImage = Image.FirstOrDefault();
if (ModelState.IsValid)
{
foreach (var item in Image)
{
if (item.Length > 0)
{
using (var stream = new MemoryStream())
{
using (Image img = Image.FromStream(uploadedImage.OpenReadStream()))
{
Stream ms = new MemoryStream(img.Resize(900, 1000).ToByteArray());
FileStreamResult fsr = new FileStreamResult(ms, "image/jpg");
uploadedImage.OpenReadStream().CopyTo(stream);
guest.Image = stream.ToArray();
guest.ContentType = uploadedImage.ContentType;
}
}
}
}
}
_context.Add(guest);
await _context.SaveChangesAsync(); }
But this scenario gives me this error:
List<IFormFile> does not contain a definition for 'FromStream' and no accessible extension method 'FromStream' accepting a first argument of type List<IFormFile> could be found.
Your received parameter named Image is the same as Image.FromStream which is in `System.Drawing`.You need to specify the namespace like below:
ImageExtension:
public static class ImageExtentions
{
public static Image Resize(this Image current, int maxWidth, int maxHeight)
{
int width, height;
if (current.Width > current.Height)
{
width = maxWidth;
height = Convert.ToInt32(current.Height * maxHeight / (double)current.Width);
}
else
{
width = Convert.ToInt32(current.Width * maxWidth / (double)current.Height);
height = maxHeight;
}
var canvas = new Bitmap(width, height);
using (var graphics = Graphics.FromImage(canvas))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(current, 0, 0, width, height);
}
return canvas;
}
public static byte[] ToByteArray(this Image current)
{
ImageConverter _imageConverter = new ImageConverter();byte[] xByte = (byte[])_imageConverter.ConvertTo(current, typeof(byte[]));return xByte;
}
}
Controller:
[HttpPost]
//[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,DoB")] Guests guest, List<IFormFile> Image)
{
IFormFile uploadedImage = Image.FirstOrDefault();
if (ModelState.IsValid)
{
foreach (var item in Image)
{
if (item.Length > 0)
{
using (var stream = new MemoryStream())
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(uploadedImage.OpenReadStream()))
{
Stream ms = new MemoryStream(img.Resize(900, 1000).ToByteArray());
FileStreamResult fsr = new FileStreamResult(ms, "image/jpg");
uploadedImage.OpenReadStream().CopyTo(stream);
guest.Image = stream.ToArray();
guest.ContentType = uploadedImage.ContentType;
}
}
}
}
}
_context.Add(guest);
await _context.SaveChangesAsync();
return View(guest);
}
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
The code I provided could work well on my project.Did you change anything?And did you change the code?Besides,what is your version of system.drawing and asp.net core?
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
62 Points
180 Posts
How to resize image before upload to varbinary column?
Apr 07, 2020 06:35 AM|musbah7@hotmail.com|LINK
Hi
I'm uploading an image to a varbinary column in the database successfully. But I want to resize it before uploading.
So this is what I tried:
Guests model:
ImageExtentions Class to resize the image:
GuestsController:
But this scenario gives me this error:
List<IFormFile> does not contain a definition for 'FromStream' and no accessible extension method 'FromStream' accepting a first argument of type List<IFormFile> could be found.
How to perform this task please?
Member
31 Points
147 Posts
Re: How to resize image before upload to varbinary column?
Apr 07, 2020 08:20 AM|1jus|LINK
Hi there,
I have found these two solutions work very well, might be worth a look:
https://www.imageflow.io/
https://imageresizing.net/
Jus
Member
62 Points
180 Posts
Re: How to resize image before upload to varbinary column?
Apr 08, 2020 07:05 AM|musbah7@hotmail.com|LINK
I've found this solution on Stack overflow:
but 'Load' gives me this error:
No overload for method Load takes 1 argument.
and I don't know to implement it in my scenario. Any help please?
Contributor
2690 Points
874 Posts
Re: How to resize image before upload to varbinary column?
Apr 08, 2020 09:14 AM|Rena Ni|LINK
Hi musbah7,
Your received parameter named Image is the same as Image.FromStream which is in `System.Drawing`.You need to specify the namespace like below:
ImageExtension:
Controller:
Best Regards,
Rena
Member
62 Points
180 Posts
Re: How to resize image before upload to varbinary column?
Apr 12, 2020 07:56 AM|musbah7@hotmail.com|LINK
Thanks.
I performed your suggestion but it gives me this error:
with this red color line of code in the error also:
Stream ms = new MemoryStream(img.Resize(266, 200).ToByteArray());
Why? and How to solve please?
Contributor
2690 Points
874 Posts
Re: How to resize image before upload to varbinary column?
Apr 24, 2020 09:22 AM|Rena Ni|LINK
Hi musbah7@hotmail.com,
The code I provided could work well on my project.Did you change anything?And did you change the code?Besides,what is your version of system.drawing and asp.net core?
Best Regards,
Rena
Member
62 Points
180 Posts
Re: How to resize image before upload to varbinary column?
May 14, 2020 07:46 AM|musbah7@hotmail.com|LINK
I'm using core 2.1 and SixLabors.ImageSharp 1.0.0-beta0007 package.