473,699 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, System.EventArg s e)
{
// Check to see if file was uploaded
if( filMyFile.Poste dFile != null )
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.Poste dFile;

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

// 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.InputStr eam.Read(myData , 0, nFileLen);

// Create a name for the file to store
string strFilename = Path.GetFileNam e(myFile.FileNa me);

// If TIFF, convert image to JPEG
//Get the patient id
string strID = Request.Form["ID"];

// Store it in database
int nFileID = WriteToDB(strFi lename, myFile.ContentT ype, ref
myData, strID);

// Set label's text
lblInfo.Text =
"Filename: " + strFilename + "<br>" +
"Size: " + nFileLen.ToStri ng() + "<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.ToStrin g();
//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.Visibl e = true;
}
}
}
Nov 18 '05 #1
1 5410
you can use load from stream

Image.FromStrea m
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 processedMemStr eam = new MemoryStream();
try
{
myStream.Write( ImageData, 0, ImageData.Lengt h);
myImage = System.Drawing. Image.FromStrea m(myStream);
int imageWidth = myImage.Width;
int imageHeight = myImage.Height;
int processedHeight ;
if(imgType == ImageType.Full)
{
processedHeight = imageWidth;
}
else
{
processedHeight = (int)imgType;
}
double multiplicationF actor =
(double)process edHeight/(double)imageHe ight;
int processedWidth = (int)( (double)imageWi dth * multiplicationF actor);

Bitmap processedBP = new Bitmap(processe dWidth, processedHeight );
Graphics g = Graphics.FromIm age(processedBP );
try
{
g.SmoothingMode =SmoothingMode. HighQuality;
g.Interpolation Mode =InterpolationM ode.HighQuality Bicubic;
g.PixelOffsetMo de =PixelOffsetMod e.HighQuality;

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

g.DrawImage(myI mage,rect,0,0,m yImage.Width,my Image.Height,Gr aphicsUnit.Pixe l
);

processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone);
processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone);

processedBP.Sav e(processedMemS tream, ImageFormat.Jpe g);
byte[] processedImageD ata = processedMemStr eam.ToArray();
if(processedIma geData != null)
{
context.Cache.A dd(cacheKey, processedImageD ata, null,
Cache.NoAbsolut eExpiration, TimeSpan.FromMi nutes(10),
CacheItemPriori ty.Normal, new
CacheItemRemove dCallback(this. RemovedCallback ));

context.Respons e.BinaryWrite(p rocessedImageDa ta);
processedImageD ata = null;
}
}
finally
{
g.Dispose();
processedBP.Dis pose();
}
}
finally
{
processedMemStr eam.Close();
myStream.Close( );
myImage.Dispose ();
}
}
--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Daniel" <da*@kenaday.co m> wrote in message
news:1b******** *************** ***@posting.goo gle.com...
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(o bject sender, System.EventArg s e)
{
// Check to see if file was uploaded
if( filMyFile.Poste dFile != null )
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.Poste dFile;

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

// 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.InputStr eam.Read(myData , 0, nFileLen);

// Create a name for the file to store
string strFilename = Path.GetFileNam e(myFile.FileNa me);

// If TIFF, convert image to JPEG
//Get the patient id
string strID = Request.Form["ID"];

// Store it in database
int nFileID = WriteToDB(strFi lename, myFile.ContentT ype, ref
myData, strID);

// Set label's text
lblInfo.Text =
"Filename: " + strFilename + "<br>" +
"Size: " + nFileLen.ToStri ng() + "<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.ToStrin g();
//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.Visibl e = true;
}
}
}

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1716
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 figuring these out.... I am in a process of moving it over to another server, however running into a few things: 1. One of the links is to upload your own logo in a BMP format and the
3
11760
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
13
2349
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 reached yet (I have gone all the way to page 50 on Google's results!!). Here are my requirements: • I have a DataGrid. Everything will be done from here. Everything. No exceptions. Everything will also be done in VB, without any code-behind to...
0
4752
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 verdiği hata şu.Bilen arkadaşlar lütfen yardım edin Persits.Upload.1 error '800a0020' The system cannot find the path specified. /classifieds/upload.asp, line 250
9
3892
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 works fine. The doctors want fewer steps to follow. So, it was asked if I can configure the browser to load/submit the image 'xray.tif' each time they click "Upload Image" instead of the doctor/animal technician having to look for for dental x-ray...
4
18826
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 jsp as front end and java servlet for the application. iam converting the image file into byte and then inserting in to data base. problem for me is iam able to do this on a local system. ie iam running the java in my local PC and able to...
21
34414
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
18
34786
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."? Have you looked around trying to find simple solutions but didn't want to wade through pages of complex code? Have you balked at paying for premade solutions that are probably overkill for your particular project? I'd like to walk you through the...
3
2244
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 gif, jpeg and png. the code that i have that can do the folowing are if($_FILES == 0){ and if (!eregi("^(+)\.jpg|\.gif|\.png$", $logoname)) but the problem is i dont know where to insert it in the code below (upload.php) i try inserting it but did not...
0
8622
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7761
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.