database driven photo gallery with upload

Discussion in 'ASP .Net' started by bob garbados, Oct 29, 2004.

  1. bob garbados

    bob garbados Guest

    I am trying to create a database-driven photo gallery for a friend with an
    admin form to upload images... I can upload a file to the web server, but I
    want to store the image in a database and I want to resize the image before
    I save it... How do I take the uploaded .jpg and shrink it to a thumbnail?
    How do I pass the uploaded .jpg to a stored procedure that will store the
    image as an image datatype in SQL Server 2000? I'm developing this without
    Visual Studio.

    <%@ Page Language="VB" %>

    <script language="VB" runat="server">

    Sub Page_Load(Source As Object, E As EventArgs)
    End Sub

    Sub Button_Click(S as Object, E as EventArgs)

    fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    togallery\NewFile.jpg")
    End Sub

    </script>
    <html>
    <head>
    <title>Image Upload</title>
    </head>
    <body>
    <form id="frmUpload" method="post" runat="server"
    enctype="multipart/form-data">
    <input type="file" id="fsoUploadFile" runat="server"><br/>
    <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    </form>
    </body>
    </html>
    bob garbados, Oct 29, 2004
    #1
    1. Advertising

  2. Here's an article I wrote that describes how to upload images into a
    database and get them back out again.
    http://steve.orr.net/content/asp200307so_f.asp

    Also, here's an image resize routine I wrote:

    /*shrink the image proportionately so that neither height nor width is more
    than [NewSize] pixels*/

    public Image ShrinkImage(Bitmap bmp, int NewSize)

    {

    double NewWidth;

    double NewHeight;

    double ShrinkPercent;

    System.Drawing.Image.GetThumbnailImageAbort myCallback =

    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

    if (bmp.Width>bmp.Height)

    {

    NewWidth=NewSize;

    ShrinkPercent=(NewWidth/bmp.Width)*100;

    NewHeight=(ShrinkPercent/100)*bmp.Height;

    }

    else

    {

    NewHeight=NewSize;

    ShrinkPercent=(NewHeight/bmp.Height)*100;

    NewWidth=(ShrinkPercent/100)*bmp.Width;

    }

    System.Drawing.Image myShrunkenImage =
    bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);

    return myShrunkenImage;

    }

    public bool ThumbnailCallback(){return false;}


    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net



    "bob garbados" <> wrote in message
    news:...
    >I am trying to create a database-driven photo gallery for a friend with an
    > admin form to upload images... I can upload a file to the web server, but
    > I
    > want to store the image in a database and I want to resize the image
    > before
    > I save it... How do I take the uploaded .jpg and shrink it to a thumbnail?
    > How do I pass the uploaded .jpg to a stored procedure that will store the
    > image as an image datatype in SQL Server 2000? I'm developing this
    > without
    > Visual Studio.
    >
    > <%@ Page Language="VB" %>
    >
    > <script language="VB" runat="server">
    >
    > Sub Page_Load(Source As Object, E As EventArgs)
    > End Sub
    >
    > Sub Button_Click(S as Object, E as EventArgs)
    >
    > fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    > togallery\NewFile.jpg")
    > End Sub
    >
    > </script>
    > <html>
    > <head>
    > <title>Image Upload</title>
    > </head>
    > <body>
    > <form id="frmUpload" method="post" runat="server"
    > enctype="multipart/form-data">
    > <input type="file" id="fsoUploadFile" runat="server"><br/>
    > <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    > </form>
    > </body>
    > </html>
    >
    >
    Steve C. Orr [MVP, MCSD], Oct 29, 2004
    #2
    1. Advertising

  3. bob garbados

    bob garbados Guest

    Thanks Steve. I actually found your article and used it as a guide to write
    my code for uploading the image to the database. I'm working on retreiving
    the images now and the resize routine looks great, I'll implement that once
    everything else is working.

    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > Here's an article I wrote that describes how to upload images into a
    > database and get them back out again.
    > http://steve.orr.net/content/asp200307so_f.asp
    >
    > Also, here's an image resize routine I wrote:
    >
    > /*shrink the image proportionately so that neither height nor width is

    more
    > than [NewSize] pixels*/
    >
    > public Image ShrinkImage(Bitmap bmp, int NewSize)
    >
    > {
    >
    > double NewWidth;
    >
    > double NewHeight;
    >
    > double ShrinkPercent;
    >
    > System.Drawing.Image.GetThumbnailImageAbort myCallback =
    >
    > new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    >
    > if (bmp.Width>bmp.Height)
    >
    > {
    >
    > NewWidth=NewSize;
    >
    > ShrinkPercent=(NewWidth/bmp.Width)*100;
    >
    > NewHeight=(ShrinkPercent/100)*bmp.Height;
    >
    > }
    >
    > else
    >
    > {
    >
    > NewHeight=NewSize;
    >
    > ShrinkPercent=(NewHeight/bmp.Height)*100;
    >
    > NewWidth=(ShrinkPercent/100)*bmp.Width;
    >
    > }
    >
    > System.Drawing.Image myShrunkenImage =
    >

    bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);
    >
    > return myShrunkenImage;
    >
    > }
    >
    > public bool ThumbnailCallback(){return false;}
    >
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "bob garbados" <> wrote in message
    > news:...
    > >I am trying to create a database-driven photo gallery for a friend with

    an
    > > admin form to upload images... I can upload a file to the web server,

    but
    > > I
    > > want to store the image in a database and I want to resize the image
    > > before
    > > I save it... How do I take the uploaded .jpg and shrink it to a

    thumbnail?
    > > How do I pass the uploaded .jpg to a stored procedure that will store

    the
    > > image as an image datatype in SQL Server 2000? I'm developing this
    > > without
    > > Visual Studio.
    > >
    > > <%@ Page Language="VB" %>
    > >
    > > <script language="VB" runat="server">
    > >
    > > Sub Page_Load(Source As Object, E As EventArgs)
    > > End Sub
    > >
    > > Sub Button_Click(S as Object, E as EventArgs)
    > >
    > >

    fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    > > togallery\NewFile.jpg")
    > > End Sub
    > >
    > > </script>
    > > <html>
    > > <head>
    > > <title>Image Upload</title>
    > > </head>
    > > <body>
    > > <form id="frmUpload" method="post" runat="server"
    > > enctype="multipart/form-data">
    > > <input type="file" id="fsoUploadFile" runat="server"><br/>
    > > <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    > > </form>
    > > </body>
    > > </html>
    > >
    > >

    >
    >
    bob garbados, Oct 29, 2004
    #3
  4. bob garbados

    bob garbados Guest

    Steve,

    I can save the image in the database, but I can't retrieve it. Here's my
    code:

    Dim con as SqlConnection
    Dim strConnectionString as String

    strConnectionString = ConfigurationSettings.AppSettings("connection")

    con= New SqlConnection(strConnectionString)

    Dim dr As System.Data.SqlClient.SqlDataReader
    Dim cmdGetPhoto as new SqlCommand("usp_GetPhotos", con)

    con.Open()
    dr = cmdGetPhoto.ExecuteReader
    If dr.Read Then
    Response.Write("Photo is here...")
    Response.Write("Photo Name: " & dr("PhotoTitle") & "<br/>")
    Response.ContentType = dr("PhotoThumbContentType").ToString
    Response.OutputStream.Write(CType(dr("PhotoThumb"), Byte()), 0,
    CInt(dr("PhotoThumbSize")))
    Response.AddHeader("Content-Disposition", dr("PhotoTitle").ToString())
    Else
    Response.Write("File Not Found.")
    End If

    'close down the connection
    con.Close()

    I stepped through the code and it executes the correct lines of code, but
    doesn't write anything and the image doesn't show up. What does the
    'inherits="CIT.ViewAttachment"' line in your code do?


    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > Here's an article I wrote that describes how to upload images into a
    > database and get them back out again.
    > http://steve.orr.net/content/asp200307so_f.asp
    >
    > Also, here's an image resize routine I wrote:
    >
    > /*shrink the image proportionately so that neither height nor width is

    more
    > than [NewSize] pixels*/
    >
    > public Image ShrinkImage(Bitmap bmp, int NewSize)
    >
    > {
    >
    > double NewWidth;
    >
    > double NewHeight;
    >
    > double ShrinkPercent;
    >
    > System.Drawing.Image.GetThumbnailImageAbort myCallback =
    >
    > new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    >
    > if (bmp.Width>bmp.Height)
    >
    > {
    >
    > NewWidth=NewSize;
    >
    > ShrinkPercent=(NewWidth/bmp.Width)*100;
    >
    > NewHeight=(ShrinkPercent/100)*bmp.Height;
    >
    > }
    >
    > else
    >
    > {
    >
    > NewHeight=NewSize;
    >
    > ShrinkPercent=(NewHeight/bmp.Height)*100;
    >
    > NewWidth=(ShrinkPercent/100)*bmp.Width;
    >
    > }
    >
    > System.Drawing.Image myShrunkenImage =
    >

    bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);
    >
    > return myShrunkenImage;
    >
    > }
    >
    > public bool ThumbnailCallback(){return false;}
    >
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "bob garbados" <> wrote in message
    > news:...
    > >I am trying to create a database-driven photo gallery for a friend with

    an
    > > admin form to upload images... I can upload a file to the web server,

    but
    > > I
    > > want to store the image in a database and I want to resize the image
    > > before
    > > I save it... How do I take the uploaded .jpg and shrink it to a

    thumbnail?
    > > How do I pass the uploaded .jpg to a stored procedure that will store

    the
    > > image as an image datatype in SQL Server 2000? I'm developing this
    > > without
    > > Visual Studio.
    > >
    > > <%@ Page Language="VB" %>
    > >
    > > <script language="VB" runat="server">
    > >
    > > Sub Page_Load(Source As Object, E As EventArgs)
    > > End Sub
    > >
    > > Sub Button_Click(S as Object, E as EventArgs)
    > >
    > >

    fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    > > togallery\NewFile.jpg")
    > > End Sub
    > >
    > > </script>
    > > <html>
    > > <head>
    > > <title>Image Upload</title>
    > > </head>
    > > <body>
    > > <form id="frmUpload" method="post" runat="server"
    > > enctype="multipart/form-data">
    > > <input type="file" id="fsoUploadFile" runat="server"><br/>
    > > <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    > > </form>
    > > </body>
    > > </html>
    > >
    > >

    >
    >
    bob garbados, Oct 29, 2004
    #4
  5. bob garbados

    bob garbados Guest

    I lied... removed the response.write and everything works beautifully.


    "bob garbados" <> wrote in message
    news:...
    > Steve,
    >
    > I can save the image in the database, but I can't retrieve it. Here's my
    > code:
    >
    > Dim con as SqlConnection
    > Dim strConnectionString as String
    >
    > strConnectionString = ConfigurationSettings.AppSettings("connection")
    >
    > con= New SqlConnection(strConnectionString)
    >
    > Dim dr As System.Data.SqlClient.SqlDataReader
    > Dim cmdGetPhoto as new SqlCommand("usp_GetPhotos", con)
    >
    > con.Open()
    > dr = cmdGetPhoto.ExecuteReader
    > If dr.Read Then
    > Response.Write("Photo is here...")
    > Response.Write("Photo Name: " & dr("PhotoTitle") & "<br/>")
    > Response.ContentType = dr("PhotoThumbContentType").ToString
    > Response.OutputStream.Write(CType(dr("PhotoThumb"), Byte()), 0,
    > CInt(dr("PhotoThumbSize")))
    > Response.AddHeader("Content-Disposition", dr("PhotoTitle").ToString())
    > Else
    > Response.Write("File Not Found.")
    > End If
    >
    > 'close down the connection
    > con.Close()
    >
    > I stepped through the code and it executes the correct lines of code, but
    > doesn't write anything and the image doesn't show up. What does the
    > 'inherits="CIT.ViewAttachment"' line in your code do?
    >
    >
    > "Steve C. Orr [MVP, MCSD]" <> wrote in message
    > news:...
    > > Here's an article I wrote that describes how to upload images into a
    > > database and get them back out again.
    > > http://steve.orr.net/content/asp200307so_f.asp
    > >
    > > Also, here's an image resize routine I wrote:
    > >
    > > /*shrink the image proportionately so that neither height nor width is

    > more
    > > than [NewSize] pixels*/
    > >
    > > public Image ShrinkImage(Bitmap bmp, int NewSize)
    > >
    > > {
    > >
    > > double NewWidth;
    > >
    > > double NewHeight;
    > >
    > > double ShrinkPercent;
    > >
    > > System.Drawing.Image.GetThumbnailImageAbort myCallback =
    > >
    > > new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    > >
    > > if (bmp.Width>bmp.Height)
    > >
    > > {
    > >
    > > NewWidth=NewSize;
    > >
    > > ShrinkPercent=(NewWidth/bmp.Width)*100;
    > >
    > > NewHeight=(ShrinkPercent/100)*bmp.Height;
    > >
    > > }
    > >
    > > else
    > >
    > > {
    > >
    > > NewHeight=NewSize;
    > >
    > > ShrinkPercent=(NewHeight/bmp.Height)*100;
    > >
    > > NewWidth=(ShrinkPercent/100)*bmp.Width;
    > >
    > > }
    > >
    > > System.Drawing.Image myShrunkenImage =
    > >

    >

    bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);
    > >
    > > return myShrunkenImage;
    > >
    > > }
    > >
    > > public bool ThumbnailCallback(){return false;}
    > >
    > >
    > > --
    > > I hope this helps,
    > > Steve C. Orr, MCSD, MVP
    > > http://Steve.Orr.net
    > >
    > >
    > >
    > > "bob garbados" <> wrote in message
    > > news:...
    > > >I am trying to create a database-driven photo gallery for a friend with

    > an
    > > > admin form to upload images... I can upload a file to the web server,

    > but
    > > > I
    > > > want to store the image in a database and I want to resize the image
    > > > before
    > > > I save it... How do I take the uploaded .jpg and shrink it to a

    > thumbnail?
    > > > How do I pass the uploaded .jpg to a stored procedure that will store

    > the
    > > > image as an image datatype in SQL Server 2000? I'm developing this
    > > > without
    > > > Visual Studio.
    > > >
    > > > <%@ Page Language="VB" %>
    > > >
    > > > <script language="VB" runat="server">
    > > >
    > > > Sub Page_Load(Source As Object, E As EventArgs)
    > > > End Sub
    > > >
    > > > Sub Button_Click(S as Object, E as EventArgs)
    > > >
    > > >

    >

    fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    > > > togallery\NewFile.jpg")
    > > > End Sub
    > > >
    > > > </script>
    > > > <html>
    > > > <head>
    > > > <title>Image Upload</title>
    > > > </head>
    > > > <body>
    > > > <form id="frmUpload" method="post" runat="server"
    > > > enctype="multipart/form-data">
    > > > <input type="file" id="fsoUploadFile" runat="server"><br/>
    > > > <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    > > > </form>
    > > > </body>
    > > > </html>
    > > >
    > > >

    > >
    > >

    >
    >
    bob garbados, Oct 29, 2004
    #5
  6. bob garbados

    bob garbados Guest

    It's Friday and I've worked too long this week, but I can't figure this one
    out... When I try to save the file to the database from my browser pointed
    at localhost, it all works fine. When I try to do the same from a remote
    machine, it doesn't work. It tries to upload the file from the server's C:\
    drive instead of the client's C:\ drive. Any ideas? Is there a setting I'm
    missing somewhere?d

    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > Here's an article I wrote that describes how to upload images into a
    > database and get them back out again.
    > http://steve.orr.net/content/asp200307so_f.asp
    >
    > Also, here's an image resize routine I wrote:
    >
    > /*shrink the image proportionately so that neither height nor width is

    more
    > than [NewSize] pixels*/
    >
    > public Image ShrinkImage(Bitmap bmp, int NewSize)
    >
    > {
    >
    > double NewWidth;
    >
    > double NewHeight;
    >
    > double ShrinkPercent;
    >
    > System.Drawing.Image.GetThumbnailImageAbort myCallback =
    >
    > new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    >
    > if (bmp.Width>bmp.Height)
    >
    > {
    >
    > NewWidth=NewSize;
    >
    > ShrinkPercent=(NewWidth/bmp.Width)*100;
    >
    > NewHeight=(ShrinkPercent/100)*bmp.Height;
    >
    > }
    >
    > else
    >
    > {
    >
    > NewHeight=NewSize;
    >
    > ShrinkPercent=(NewHeight/bmp.Height)*100;
    >
    > NewWidth=(ShrinkPercent/100)*bmp.Width;
    >
    > }
    >
    > System.Drawing.Image myShrunkenImage =
    >

    bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);
    >
    > return myShrunkenImage;
    >
    > }
    >
    > public bool ThumbnailCallback(){return false;}
    >
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "bob garbados" <> wrote in message
    > news:...
    > >I am trying to create a database-driven photo gallery for a friend with

    an
    > > admin form to upload images... I can upload a file to the web server,

    but
    > > I
    > > want to store the image in a database and I want to resize the image
    > > before
    > > I save it... How do I take the uploaded .jpg and shrink it to a

    thumbnail?
    > > How do I pass the uploaded .jpg to a stored procedure that will store

    the
    > > image as an image datatype in SQL Server 2000? I'm developing this
    > > without
    > > Visual Studio.
    > >
    > > <%@ Page Language="VB" %>
    > >
    > > <script language="VB" runat="server">
    > >
    > > Sub Page_Load(Source As Object, E As EventArgs)
    > > End Sub
    > >
    > > Sub Button_Click(S as Object, E as EventArgs)
    > >
    > >

    fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
    > > togallery\NewFile.jpg")
    > > End Sub
    > >
    > > </script>
    > > <html>
    > > <head>
    > > <title>Image Upload</title>
    > > </head>
    > > <body>
    > > <form id="frmUpload" method="post" runat="server"
    > > enctype="multipart/form-data">
    > > <input type="file" id="fsoUploadFile" runat="server"><br/>
    > > <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
    > > </form>
    > > </body>
    > > </html>
    > >
    > >

    >
    >
    bob garbados, Oct 29, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bob garbados

    Photo Gallery

    bob garbados, Nov 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    640
    Steve C. Orr [MVP, MCSD]
    Nov 2, 2004
  2. dan
    Replies:
    1
    Views:
    356
    bruce barker
    Dec 13, 2004
  3. msnews
    Replies:
    4
    Views:
    1,763
  4. rpm
    Replies:
    4
    Views:
    447
    clintonG
    Dec 27, 2005
  5. Xah Lee
    Replies:
    1
    Views:
    440
    Xah Lee
    Apr 19, 2006
Loading...

Share This Page