Solved

Upload-Resize-Crop Image In SQL Databse in Binary Format in ASP.Net using C#

Posted on 2009-04-11
4
770 Views
Last Modified: 2013-11-26
I want to upload the Multiple images at a time(on button click)(using flash uploder or any other) in SQL databse where the images saves in binary format.(Please not that I am not storing the images path in database) and also i want the images should be resized and crop before upload.
<email deleted>

Thanks!
Vikas.
0
Comment
Question by:SCALERCODE
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 24129596
I should probably leave this to the admins, but please note that solutions are supposed to be posted right here on Experts Exchange - that way they can help more than one person.

You might want to be specific about the type of database you are using (do you mean SQLServer database?)
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24130569
The page that handles the file upload post can get the InputStream for each file from the Request.Files object.

A method like this will take the inputStream and generate a byte array that contains the thumbnail data. It's then a simple matter of inserting the binary data into your database
    private byte[] GetThumbBytes(Stream inputStream, Size maxSize) {

      Bitmap original = new Bitmap(inputStream);

      double scale = Math.Min(maxSize.Width / original.Width, maxSize.Height, original.Height);

      Bitmap thumb = new Bitmap(original, (int)(scale * original.Width), (int)(scale * original.Height));
 

      // Save the thumb to a memorystream

      MemoryStream ms = new MemoryStream();

      thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

      byte[] buff = ms.ToArray();
 

      original.Dispose();

      thumb.Dispose();

      ms.Dispose();
 

      return buff;

    }

Open in new window

0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24133828
Minor mistake, the scale is calcualated incorrectly. The numerators have to be cast as double, else scale ends up being to the nearest integer which isn't ideal.
    private byte[] GetThumbBytes(Stream inputStream, Size maxSize) {

      Bitmap original = new Bitmap(inputStream);

      double scale = Math.Min((double)maxSize.Width / original.Width, (double)maxSize.Height / original.Height);

      Bitmap thumb = new Bitmap(original, (int)(scale * original.Width), (int)(scale * original.Height));

 

      // Save the thumb to a memorystream

      MemoryStream ms = new MemoryStream();

      thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

      byte[] buff = ms.ToArray();

 

      original.Dispose();

      thumb.Dispose();

      ms.Dispose();

 

      return buff;

    }

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now