By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,685 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,685 IT Pros & Developers. It's quick & easy.

Upload file and convert image before inserting to db

P: n/a
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;
}
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.Rotate180Fli pNone);
processedBP.RotateFlip(RotateFlipType.Rotate180Fli pNone);

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" <da*@kenaday.com> wrote in message
news:1b**************************@posting.google.c om...
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;
}
}
}

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.