Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-04-11
4
Medium Priority
?
936 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

604 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