Upload file and convert image before inserting to db

Discussion in 'ASP .Net' started by Daniel, Oct 4, 2004.

  1. Daniel

    Daniel Guest

    I have looked everywhere on the web for an answer to this and the only
    thing I can find is converting the image format when the file is
    present on the local filesystem. What I want to do is use a web form
    to upload a TIFF image (scanned images) and convert it to a JPEG
    before I insert it into SQL Server. The file upload part works great,
    but I can;t convert the image. I certainly don't want to upload it the
    the server filesystem, convert it, upload it and then delete it. That
    would just be too expensive.

    I'm pretty new at using .net and it seems like it should be easy
    enough to do, but I can't get it to work for the life of me. I will
    post my code below to help.

    // Processes click on our cmdSend button
    private void cmdSend_Click(object sender, System.EventArgs e)
    {
    // Check to see if file was uploaded
    if( filMyFile.PostedFile != null )
    {
    // Get a reference to PostedFile object
    HttpPostedFile myFile = filMyFile.PostedFile;

    // Get size of uploaded file
    int nFileLen = myFile.ContentLength;

    // make sure the size of the file is > 0
    if( nFileLen > 0 )
    {
    // Allocate a buffer for reading of the file
    byte[] myData = new byte[nFileLen];

    // Read uploaded file from the Stream
    myFile.InputStream.Read(myData, 0, nFileLen);

    // Create a name for the file to store
    string strFilename = Path.GetFileName(myFile.FileName);

    // If TIFF, convert image to JPEG


    //Get the patient id
    string strID = Request.Form["ID"];

    // Store it in database
    int nFileID = WriteToDB(strFilename, myFile.ContentType, ref
    myData, strID);

    // Set label's text
    lblInfo.Text =
    "Filename: " + strFilename + "<br>" +
    "Size: " + nFileLen.ToString() + "<p>";


    // Set URL of the the object to point to the this script with ID
    of the file
    // that will retreive file out the database
    //imgDB.ImageUrl = GetMyName() + "?FileID=" + nFileID.ToString();
    //imgDB.ToolTip = "This file was stored in database.";
    lblText2.Text = "File was uploaded successfully.";

    // show the images and text
    //imgDB.Visible = true;
    lblInfo.Visible = true;
    lblText2.Visible = true;
    }
    }
    }
    Daniel, Oct 4, 2004
    #1
    1. Advertising

  2. Daniel

    Hermit Dave Guest

    you can use load from stream

    Image.FromStream
    once you have the image use the code similar to resizing.

    copying an old piece of code that i had posted a few weeks back I am
    resizing a jpeg file. You should be alright loading the file
    and then using its image width and height to create a new bitmap... load the
    image and do whatever you need to do.

    Hope this helps.....................................

    private void WriteImage(ref byte[] ImageData, ref HttpContext context,
    ImageType imgType, string cacheKey)
    {
    System.Drawing.Image myImage = null;
    MemoryStream myStream = new MemoryStream();
    MemoryStream processedMemStream = new MemoryStream();
    try
    {
    myStream.Write(ImageData, 0, ImageData.Length);
    myImage = System.Drawing.Image.FromStream(myStream);
    int imageWidth = myImage.Width;
    int imageHeight = myImage.Height;
    int processedHeight;
    if(imgType == ImageType.Full)
    {
    processedHeight = imageWidth;
    }
    else
    {
    processedHeight = (int)imgType;
    }
    double multiplicationFactor =
    (double)processedHeight/(double)imageHeight;
    int processedWidth = (int)( (double)imageWidth * multiplicationFactor);

    Bitmap processedBP = new Bitmap(processedWidth, processedHeight);
    Graphics g = Graphics.FromImage(processedBP);
    try
    {
    g.SmoothingMode =SmoothingMode.HighQuality;
    g.InterpolationMode =InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode =PixelOffsetMode.HighQuality;

    Rectangle rect=new Rectangle(0,0,processedWidth,processedHeight);
    //Draw the old image on to the new image using the graphics object:

    g.DrawImage(myImage,rect,0,0,myImage.Width,myImage.Height,GraphicsUnit.Pixel
    );

    processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);
    processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);

    processedBP.Save(processedMemStream, ImageFormat.Jpeg);
    byte[] processedImageData = processedMemStream.ToArray();
    if(processedImageData != null)
    {
    context.Cache.Add(cacheKey, processedImageData, null,
    Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
    CacheItemPriority.Normal, new
    CacheItemRemovedCallback(this.RemovedCallback));

    context.Response.BinaryWrite(processedImageData);
    processedImageData = null;
    }
    }
    finally
    {
    g.Dispose();
    processedBP.Dispose();
    }
    }
    finally
    {
    processedMemStream.Close();
    myStream.Close();
    myImage.Dispose();
    }
    }


    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "Daniel" <> wrote in message
    news:...
    > I have looked everywhere on the web for an answer to this and the only
    > thing I can find is converting the image format when the file is
    > present on the local filesystem. What I want to do is use a web form
    > to upload a TIFF image (scanned images) and convert it to a JPEG
    > before I insert it into SQL Server. The file upload part works great,
    > but I can;t convert the image. I certainly don't want to upload it the
    > the server filesystem, convert it, upload it and then delete it. That
    > would just be too expensive.
    >
    > I'm pretty new at using .net and it seems like it should be easy
    > enough to do, but I can't get it to work for the life of me. I will
    > post my code below to help.
    >
    > // Processes click on our cmdSend button
    > private void cmdSend_Click(object sender, System.EventArgs e)
    > {
    > // Check to see if file was uploaded
    > if( filMyFile.PostedFile != null )
    > {
    > // Get a reference to PostedFile object
    > HttpPostedFile myFile = filMyFile.PostedFile;
    >
    > // Get size of uploaded file
    > int nFileLen = myFile.ContentLength;
    >
    > // make sure the size of the file is > 0
    > if( nFileLen > 0 )
    > {
    > // Allocate a buffer for reading of the file
    > byte[] myData = new byte[nFileLen];
    >
    > // Read uploaded file from the Stream
    > myFile.InputStream.Read(myData, 0, nFileLen);
    >
    > // Create a name for the file to store
    > string strFilename = Path.GetFileName(myFile.FileName);
    >
    > // If TIFF, convert image to JPEG
    >
    >
    > //Get the patient id
    > string strID = Request.Form["ID"];
    >
    > // Store it in database
    > int nFileID = WriteToDB(strFilename, myFile.ContentType, ref
    > myData, strID);
    >
    > // Set label's text
    > lblInfo.Text =
    > "Filename: " + strFilename + "<br>" +
    > "Size: " + nFileLen.ToString() + "<p>";
    >
    >
    > // Set URL of the the object to point to the this script with ID
    > of the file
    > // that will retreive file out the database
    > //imgDB.ImageUrl = GetMyName() + "?FileID=" + nFileID.ToString();
    > //imgDB.ToolTip = "This file was stored in database.";
    > lblText2.Text = "File was uploaded successfully.";
    >
    > // show the images and text
    > //imgDB.Visible = true;
    > lblInfo.Visible = true;
    > lblText2.Visible = true;
    > }
    > }
    > }
    Hermit Dave, Oct 4, 2004
    #2
    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. Steve Lloyd

    Resize image before upload

    Steve Lloyd, Oct 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,356
    Kevin Spencer
    Oct 20, 2004
  2. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,407
    Bruce Barker
    Aug 3, 2005
  3. jmhmaine

    Inserting Text before the Numberic Page numbers in paging

    jmhmaine, Feb 28, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    144
    Ken Cox [Microsoft MVP]
    Feb 28, 2005
  4. Filip De Backer

    Check if image file exists before linking to Image object

    Filip De Backer, Jun 11, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    294
    Filip De Backer
    Jun 11, 2004
  5. Lisa
    Replies:
    11
    Views:
    200
    Gunnar Hjalmarsson
    Dec 21, 2004
Loading...

Share This Page