473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Upload file and convert image before inserting to db

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
1 5377
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eugene Borukhovich | last post by:
Hello, I just got a client that is running their PHP site on Apache 1.3 with PHP 4.x. The server is very weak and during peak times hangs. I am not a programmer, so having a little dificulty...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
13
by: Neo Geshel | last post by:
I have examined about 80+ different upload scripts on the 'net, both in VB and C#, and none seem to do what I need them to do. Perhaps someone here can point me somewhere that Google hasn't...
0
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle...
9
by: Steve Poe | last post by:
I work for an animal hospital trying to use PHP to store an animal's dental x-rays to a file server. I can browse for the xray on the local desktop computer then click "Upload Image". This...
4
by: meseenu | last post by:
Hi every one, iam developing an application where i want to upload image and video files in to a oracle data base. I have used BLOB data type to store the image and video files. iam using...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
18
jhardman
by: jhardman | last post by:
Have you ever wanted to upload files through a form and thought, "I'd really like to use ASP, it surely has that capability, but the tutorial I used to learn ASP didn't mention how to do this."? ...
3
by: simon2x1 | last post by:
the code below will upload image and insert the image name and other variable into the database but i dont want the upload feild to post a blank field and also not to allow any image which is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.