Click here to Skip to main content
12,402,202 members (66,136 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.3K views
8 bookmarked
Posted

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

, 17 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

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('\\'));
    //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.  Smile | :)  

License

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

Share

About the Author

Ghalib Mirza
Software Developer Zee Solutions
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionMessage Automatically Removed Pin
17-Sep-13 17:51
groupbernice12317-Sep-13 17:51 
QuestionMore explanation Pin
Mika Wendelius17-Sep-13 8:25
mvpMika Wendelius17-Sep-13 8:25 
AnswerRe: More explanation Pin
GhalibMirza18-Sep-13 5:53
professionalGhalibMirza18-Sep-13 5:53 
Generalgreat way to create your photogallery Pin
NitinSingh17-Sep-13 8:06
memberNitinSingh17-Sep-13 8:06 
GeneralRe: great way to create your photogallery Pin
GhalibMirza18-Sep-13 5:55
professionalGhalibMirza18-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 17 Sep 2013
Article Copyright 2013 by Ghalib Mirza
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid