Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, January 15, 2020 7:58 PM
I would like to reduce the file size of images I am uploading to a SQL Database and / or possibly prohibit any images larger then xx size. But when reducing the size I would like to maintain the ratio. Is this possible. Below is the code from the controller and the view:
@Html.LabelFor(model => model.files, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.files, "", new { @type = "file", @multiple = "multiple" })
@Html.ValidationMessageFor(model => model.files, "", new { @class = "text-danger" })
</div>
[HttpPost]
public ActionResult Create(Shop model, HttpPostedFileBase[] files)
{
foreach (HttpPostedFileBase file in files)
{
if (file != null)
{
var fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, file.ContentLength);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
All replies (7)
Thursday, January 16, 2020 2:01 AM
I would like to reduce the file size of images I am uploading to a SQL Database and / or possibly prohibit any images larger then xx size. But when reducing the size I would like to maintain the ratio.
So there are two things here, one involves limiting the size of files that can be uploaded, which can be done at the client-level via the File API and at the server-level by simply just checking the size of the file(s):
// Example of File API
<input id='files' type='file' onchange='checkSize(this)' />
<script>
function checkSize(input){
alert('This file size is ' + input.files[0].size)
}
</script>
You could use this to verify if a specific file as too large and prevent the user from uploading it. Client-side validation is not really going to be ideal however, and you are much better at simply rejecting it at the server by reading the size of the file directly from the Length property.
if (file != null)
{
var fileBytes = new byte[file.ContentLength];
if (fileBytes > MAX_FILE_SIZE)
{
// Do something
}
// Otherwise save
}
You can find an excellent example here that discusses implementing both client and server validation using attributes.
But when reducing the size I would like to maintain the ratio.
This is a different problem altogether if you are planning on altering the sizes of the files. If your chief concern is simply size, I'd recommend using some form of compression and simply compressing the file down to fall under if your size limitation. Using a severe level of lossy compression can greatly reduce the size of even very large files, while still maintaining the size of the image itself (e.g. length x width)
Thursday, January 16, 2020 7:46 PM
So I am a little confused. I am trying the following link from the link that was above: http://b9dev.blogspot.com/2013/06/nets-built-in-jpeg-encoder-convenient.html
This example compresses and save to a file location. How could I do that and save to a Database (varbinary)
var jpegCodec = ImageCodecInfo.GetImageEncoders().First(enc => enc.FormatID == ImageFormat.Jpeg.Guid);
var jpegParams = new EncoderParameters(1);
var fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, file.ContentLength);
Thursday, January 16, 2020 9:19 PM
in general system.drawing should not be used with a web site (its designed for desktop use). there are a lot of image resize libraries. but if you want use system drawing, then you need to load the filestream into an image, inspect the quality setting, and save.
/en-us/dotnet/api/system.drawing.image.fromstream?view=netframework-4.8
Thursday, January 16, 2020 10:03 PM
Thanks. I will look at that. I don't need to use system.drawing - I just want a simple solution to reduce the file size if needed.
Friday, January 17, 2020 9:21 AM
Hi Baze72,
You can take a look at WebImage, you can use the resize() method to resize your picture, and this link may help you
https://stackoverflow.com/questions/14613325/resize-image-width-in-c-sharp-but-not-the-height
Best Regards,
Jiadong Meng
Friday, January 17, 2020 2:41 PM
Any thoughts, the controller does not like Resize??
file.Resize(800, 100000, true, true);
I have added
using System.Web.Helpers;
Saturday, January 18, 2020 2:04 AM
You might want to consider using the ImageProcessor library, which is freely available and should work for web applications. After adding the NuGet package, you should be able to call the `Resize()` method to resize any of your images and the `Quality()` one to handle reducing file sizes via compression:
byte[] photoBytes = File.ReadAllBytes(file);
// Format is automatically detected though can be changed.
ISupportedImageFormat format = new JpegFormat { Quality = 70 };
Size size = new Size(150, 0)
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData:true))
{
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Resize(size)
.Format(format)
.Save(outStream);
}
// Do something with the stream.
}
}