Click here to Skip to main content
15,891,942 members
Articles / Multimedia / GDI+
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
17 Sep 2013CPOL 13.9K   8   5
Image Handeling in C#

Introduction

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:

C#
using System.Drawing.Imaging;
using System.Drawing.Drawing2D; 

Write the following line globally in the form:

C#
public Size OriginalImageSize { get; set; } 

Now create some functions:

C#
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);
    }
    else
    {
        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);

    }
    else
    {
        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:

C#
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.. :)

C#
private String imageUpload()
{
    string imagepath = pictureBox2.ImageLocation.ToString();
    string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
    //MessageBox.Show(picname);

    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);
    }
    else
    {
        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.  :) 

License

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


Written By
Software Developer Zee Solutions
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMore explanation Pin
Wendelius17-Sep-13 8:25
mentorWendelius17-Sep-13 8:25 
AnswerRe: More explanation Pin
Ghalib Mirza18-Sep-13 5:53
professionalGhalib Mirza18-Sep-13 5:53 
Generalgreat way to create your photogallery Pin
Nitin Singh India17-Sep-13 8:06
Nitin Singh India17-Sep-13 8:06 
GeneralRe: great way to create your photogallery Pin
Ghalib Mirza18-Sep-13 5:55
professionalGhalib Mirza18-Sep-13 5:55 

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

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