Click here to Skip to main content
Click here to Skip to main content
Go to top

Store image to local folder after resizing and naming in SQL Server database in C#

, 17 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Image Handeling in C#


While creating my Student Management System I have to deal with student image & there my scenario is to store student image to a local folder situated in my project namespace after resizing and its name (only) to database, in order to be lighten the database.. So, I do search for that almost 5 days and finally I succeeded and I m sharing it with you. Have a look...

First of all you have to import two extra libraries:

using System.Drawing.Imaging;
using System.Drawing.Drawing2D; 

Write the following line globally in the form:

public Size OriginalImageSize { get; set; } 

Now create some functions:

static Image ScaleByPercent(Image imgPhoto)
    int sourceWidth = imgPhoto.Width;     //store original width of source image.
    int sourceHeight = imgPhoto.Height;   //store original height of source image.
    int sourceX = 0;        //x-axis of source image.
    int sourceY = 0;        //y-axis of source image.
    int destX = 0;          //x-axis of destination image.
    int destY = 0;          //y-axis of destination image.
    Bitmap bmPhoto;

    if (sourceWidth > sourceHeight)
    //for cheking whether slected image orizontally or vertically oriented
        bmPhoto = new Bitmap(300, 200, PixelFormat.Format24bppRgb);
        bmPhoto = new Bitmap(200, 300, PixelFormat.Format24bppRgb);

    //Create a graphics object and set quality of graphics.
    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
    //Draw image by using DrawImage() method of graphics class.
    int bmPhotowidth=bmPhoto.Width;
    int bmPhotoHeight = bmPhoto.Height;
    if (bmPhotowidth > bmPhotoHeight)
        grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, 300, 200), 
          new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

        grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, 200, 300), 
          new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

    grPhoto.Dispose();  //Dispose graphics object.
    return bmPhoto;

Now use button "browsebtn_click" event to select image from computer:

private void button2_Click(object sender, EventArgs e)
    OpenFileDialog opd = new OpenFileDialog();
    if (opd.ShowDialog()==DialogResult.OK)
        pictureBox2.ImageLocation = opd.FileName;
        int picwidth = pictureBox2.Image.Width;
        int picheight = pictureBox2.Image.Height;
        string imgsize = "Original Image Width = " + 
                 picwidth + " And Height = " + picheight;
        OriginalImageSize = new Size(picwidth, picheight);

Now write a new function to store the name into database. Here I am just showing you to save image to folder and get the file name that u can store to database. I guess you know the Insert Query.. Smile | :)

private String imageUpload()
    string imagepath = pictureBox2.ImageLocation.ToString();
    string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));

    Image scaledImage = ScaleByPercent(pictureBox2.Image);
    pictureBox2.Image = scaledImage;

    string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
    Bitmap imgImage = new Bitmap(pictureBox2.Image);    //Create an object of Bitmap class/
    if (!Directory.Exists(path + "\\images\\"))
        Directory.CreateDirectory(path + "\\images\\");
        imgImage.Save(path + "\\images\\" + picname);
        imgImage.Save(path + "\\images\\" + picname);
    return picname;//this file name can be accesible to your save button click event.


So, there you go. Hope you like this article.  Smile | :)  


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ghalib Mirza
Software Developer Zee Solutions
Pakistan Pakistan
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

QuestionMessage Automatically Removed Pingroupbernice12317-Sep-13 17:51 
QuestionMore explanation PinmvpMika Wendelius17-Sep-13 8:25 
AnswerRe: More explanation PinprofessionalGhalibMirza18-Sep-13 5:53 
Generalgreat way to create your photogallery PinmemberNitinSingh17-Sep-13 8:06 
GeneralRe: great way to create your photogallery PinprofessionalGhalibMirza18-Sep-13 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 17 Sep 2013
Article Copyright 2013 by Ghalib Mirza
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid