hi all,
i am trying to create a thubnail image and want to insert in a folder and path into database.. i uploaded image into folder but not able to create thumbnail from it

string s = @"~\uploads\" + FileUpload1.FileName;
            
            FileUpload1.PostedFile.SaveAs(Server.MapPath(s));

          

            SqlConnection cn = new SqlConnection("Server=praveen;Integrated Security=True;User Id=sa;Password=sa;Initial Catalog=banking");
            string sql = "insert into products values('" + ddlist.SelectedItem.Value + "','" + name.Text + "','" + s + "','" + code.Text + "')";
           
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader dtr;
            dtr = cmd.ExecuteReader();

iam uploading into uploads folder and wanted to upload thumbnail into thumb folder which is in uploads folder itself.. i tried various scripts found on net but nothing worked..
can anybody help me please..
thank you

adatapost posted a good article if you want to do some cool things to the image as you're creating a thumbnail. If all you want to do is resize an image, the code is fairly simple. Here is a class I use whenever I need to do some resizing. It supports replacing an existing image as well as providing a new name (via method overloading).

// uses System.Drawing namespace
public class ImageResizer
{
    public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth)
    {
        return this.ResizeImage(fullFileName, maxHeight, maxWidth, fullFileName);
    }

    public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth, string newFileName)
    {
        Image originalImage = Image.FromFile(fullFileName);

        int height = originalImage.Height;
        int width = originalImage.Width;
        int newHeight = maxHeight;
        int newWidth = maxWidth;

        if (height > maxHeight || width > maxWidth)
        {
            if (height > maxHeight)
            {
                newHeight = maxHeight;
                float temp = ((float)width / (float)height) * (float)maxHeight;
                newWidth = Convert.ToInt32(temp);

                height = newHeight;
                width = newWidth;
            }

            if (width > maxWidth)
            {
                newWidth = maxWidth;
                float temp = ((float)height / (float)width) * (float)maxWidth;
                newHeight = Convert.ToInt32(temp);
            }

            Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero);
            originalImage.Dispose();
            resizedImage.Save(newFileName);

            return true;
        }
        else if (fullFileName != newFileName)
        {
            // no resizing necessary, but need to create new file
            originalImage.Save(newFileName);
        }

        return false;
    }

    private bool ThumbnailCallback()
    {
        return false;
    }
}
commented: thank you very much +2

thank you very much for reply but i got error in following

The method or operation is not implemented.
internal string ResizeImage(string s, string p, string p_3)
            {
                throw new NotImplementedException();
            }

iam new to .net, not able to understand what it is

thank you..

>but i got error in following

Error says that you didn't implement this method.

Take a look at apegram's post. For more detail see - Image.GetThumbnailImage MSDN link.

This is something that I use, and it works quite well.

Throw a FileUpload control on your form, and add in in the code behind.
This should work stright out of the box.
I just copied and pasted it in, if you have any issues please let me know.

Carrzkiss

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Drawing.Drawing2D
protected void UploadButton_Click(object sender, EventArgs e) 
{ 
    if (FileUploadControl.HasFile) 
    { 
        try 
        { 
            if (FileUploadControl.PostedFile.ContentType.Contains("image")) 
            { 
                if (FileUploadControl.PostedFile.ContentLength < 102400) 
                { 
                    string filename = Path.GetFileName(FileUploadControl.FileName); 
                    FileUploadControl.SaveAs(Server.MapPath("~/Uploads/") + filename); 
                    StatusLabel.Text = "Upload status: Original File uploaded! proceeding to produce thumb nail <br/>"; 
                    System.Drawing.Image ImageToSave = resizeImage(System.Drawing.Image.FromStream(FileUploadControl.FileContent), new Size(130, 150));// supply actual image to the function 
                    ImageToSave.Save(Server.MapPath(System.IO.Path.Combine("~/Uploads/", filename.Insert(filename.LastIndexOf('.'),"-T"))));// the function returns a thumbnail which is saved here  
                    StatusLabel.Text += "Upload status: Thumbnail produced..."; 


        Dim objConnection As OleDbConnection = Nothing
        Dim objCmd As OleDbCommand = Nothing
        Dim strConnection As [String], strSQL As [String]
        
        strConnection = "Provider=SQLOLEDB;Data Source=MACHINE-NAME;Database=DATABASENAME;User ID=USERNAME;Password=********;"
        
        objConnection = New OleDbConnection(strConnection)
        objConnection.ConnectionString = strConnection
        // change this to work for you.
        objConnection.Open()
        strSQL = "INSERT INTO MegaPics(PicsPath, PicsSize)VALUES(?,?)"
        objCmd = New OleDbCommand(strSQL, objConnection)
        objCmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@PicsPath", getYear + clear))
        objCmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@PicsSize", bytes.Length))
        objCmd.ExecuteNonQuery()

                } 
                else 
                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!"; 
            } 
            else 
                StatusLabel.Text = "Upload status: Only JPEG files are accepted!"; 
        } 
        catch (Exception ex) 
        { 
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
        } 
    } 
} 
 
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) 
{ 
    int sourceWidth = imgToResize.Width; 
    int sourceHeight = imgToResize.Height; 
    float nPercent = 0; 
    float nPercentW = 0; 
    float nPercentH = 0; 
 
    nPercentW = ((float)size.Width / (float)sourceWidth); 
    nPercentH = ((float)size.Height / (float)sourceHeight); 
 
    if (nPercentH < nPercentW) 
        nPercent = nPercentH; 
    else 
        nPercent = nPercentW; 
 
    int destWidth = (int)(sourceWidth * nPercent); 
    int destHeight = (int)(sourceHeight * nPercent); 
 
    Bitmap b = new Bitmap(destWidth, destHeight); 
    Graphics g = Graphics.FromImage((System.Drawing.Image)b); 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
 
    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); 
    g.Dispose(); 
 
    return (System.Drawing.Image)b; 
}

thank you for your reply
i got error in following line

The method or operation is not implemented.
int destHeight = (int)(sourceHeight * nPercent);

i am pasting whole code please correct me

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace sql.admin
{
    public partial class addproducts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            binddropdown();

        }
        // uses System.Drawing namespace
       
        public void binddropdown()
        {
            SqlConnection conn = new SqlConnection("Server=praveen-4432f36;Integrated Security=True;User Id=sa;Password=sa;Initial Catalog=banking");
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            da.SelectCommand = new SqlCommand("select name,id from Categories", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "data");
            ddlist.AppendDataBoundItems = true;
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem();
            li.Text = "Select";
            li.Value = "Select";
            ddlist.Items.Add(li);
            ddlist.DataSource = ds.Tables[0].DefaultView;
            ddlist.DataTextField = "name";
            ddlist.DataValueField = "id";
            ddlist.DataBind();

        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            string filename = Path.GetFileName(FileUpload1.FileName); 
            string s = @"~\uploads\" + FileUpload1.FileName;
            
            FileUpload1.PostedFile.SaveAs(Server.MapPath(s));

            System.Drawing.Image ImageToSave = resizeImage(System.Drawing.Image.FromStream(FileUpload1.FileContent), new Size(130, 150));// supply actual image to the function 
            string thumb =ImageToSave.Save(Server.MapPath(System.IO.Path.Combine("~/uploads/thumb/", filename.Insert(filename.LastIndexOf('.'), "-T"))));
            SqlConnection cn = new SqlConnection("Server=praveen-4432f36;Integrated Security=True;User Id=sa;Password=sa;Initial Catalog=banking");
            string sql = "insert into products values('" + ddlist.SelectedItem.Value + "','" + name.Text + "','" + thumb + "','" + code.Text + "')";
           
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader dtr;
            dtr = cmd.ExecuteReader();


        }
        private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((System.Drawing.Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (System.Drawing.Image)b;
        }


    }
}

I am just learning asp.net myself. This is only my 2nd week in it.

What I usually do is make up demo's for people to learn by.
Please download this example and run it.
This comes with an Access Database and the code you need to run the project.

http://www.cffcs.com/Tutorials/ASPNETCsharpUwT&DI.zip

Please study the code and then implement the SQL Server code inplace of the Access Database code.
And I am sure you will be pleased.

Please let me know if you have any issues.

Good Luck
Carrzkiss

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.