Getting image height and width when image is saved in database - asp.net

I save my images into my SQL Server Database with ASP.NET(2.0).
(imageData -> image) (imageType -> varchar) (imageLength -> bigint)
Thus the imageData will be "Binary data" and the imageType will be like "image/gif" and the imageLength will be like "6458".......
Is it possible to get the image HEIGHT and WIDTH from my VB.NET code inside my ASP.NET?
I want to make my picture box on my web form the size of the actual image that is saved in my database.
Regards
Etienne

Assuming you have the data in a stream:
System.Drawing.Image.FromStream(yourStream).Height
You are probally better doing this when you save the image to the DB, as I'm sure that loading the image object isn't going to be cheap.
Edit
If we take this to email then the next guy with this issue won't have a record of our solution. Let's keep it in the forum for now.
Just so we know, I am a C# developer so I'm not going to try and remember vb.net syntax if this is an issue and you need help converting let me know.
You have an IDataReader I'm assuming which is pulling an Image or binary varbinary etc field from your DB. You need to load it into an object which derives from System.IO.Stream. For our purposes a MemoryStream is the perfect choice as it doesn't require a backing store such as a disk.
System.IO.MemoryStream yourStream = new System.IO.MemoryStream(dr["imgLength"] as byte[]);
System.Drawing.Image yourImage=System.Drawing.Image.FromStream(yourStream);
yourImage.Height;
yourImage.width

I would save the height and width of the image in separate columns when you save the image to the database intially. Then when you do your select statement to read the image out of the database, you can also access the height and width fields from the database.
Alternatively you can access the height and width information when you load the image out of the database and then set the height and width properties before assigning the image to the picture box.

You can get the height and width but you are going to have to load the whole image into memory using the system.drawing.image library, each time you need that info.
Better to save it as separate fields the first time you save it to the database, that is generally what I do.

Sounds like you want 2 more fields in your database, height and width.

I strongly believe that after a few hundred gigabytes of images you'll find yourself thinking that the file system and static file http servers are better suited than the databas for storing images. It also allows you to use thousands of existing free tools to work with, move, host, etc the images. Your database might get busy, and it's not easy to cluster.

Dim mobj_wc As New System.Net.WebClient
Dim obj_BookOriginalImage As System.Drawing.Bitmap
Dim ImageInBytes() As Byte = mobj_wc.DownloadData(mstr_BookURL & mds_BookDetails.Tables(0).Rows(0).Item("BookImage"))
'CREATE A MEMORY STREAM USING THE BYTES
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
obj_BookOriginalImage = New System.Drawing.Bitmap(ImageStream)
mint_ImageWidth = obj_BookOriginalImage.Width

Related

Can I test the validity of an image file before uploading it in ASP.NET?

I have an ASP.NET web application that allows the user to upload a file from his PC to a SQL Server database (which is later used to generate an image for an tag). Is there an "easy" way to test the image within .NET to validate that it does not contain anything malicious before saving it?
Right now, I use this:
MemoryStream F = new MemoryStream();
Bitmap TestBitmap = new Bitmap(Filename);
TestBitmap.Save(F, System.Drawing.Imaging.ImageFormat.Png);
int PhotoSize = (int)F.Length;
Photo = new byte[PhotoSize];
F.Seek(0, SeekOrigin.Begin);
int BytesRead = F.Read(Photo, 0, PhotoSize);
F.Close();
Creating TestBitmap fails if it is not an image (e.g. if Filename is the name of a text file), but apparently this doesn't stop a file that is an image with malicious code appended to it from loading as an image, so saving it as a MemoryStream and then writing the stream to a byte array (which is later saved in the database) supposedly fixes this.
To avoid people pass programs and other information's using the ability to upload photos to your site you can do two main steps.
Read and save again the image with your code to remove anything elst.
Limit the size of each image to a logical number.
To avoid some one upload bad code and run it on your server you keep an isolate folder with out permission to run anything. More information's about that on:
I've been hacked. Evil aspx file uploaded called AspxSpy. They're still trying. Help me trap them‼
And a general topic on the same subject: Preparing an ASP.Net website for penetration testing

Retrieve image stored in in db and save to a file

I have a customer that is asking to be able to retrieve an image from a Sql Server Db and turn around and save it to a directory on his computer. He wants to be able to do this via a asp.net web page. I am able to retrieve the image from the image as a byte array but have no idea how to take that image and save it to the directory as a file. Anyone have any ideas on how to do this? Thanks.
You can try with Save Method
MemoryStream memoryStream = new MemoryStream((byte[])YourDataTable.Rows[0]["ImageData"]);
Picturebox picturebox = new Picturebox();
picturebox.Image = Image.FromStream(memoryStream);
picturebox.Image.Save("...YourPath", System.Drawing.Imaging.ImageFormat.Jpeg)

Display DB blob type in Flex

My Mysql database stores images (in PNG, JPG)of our personnel and it's field type is set to longblob.
Is there any possibility to load blob data type using HttpService and render it in Image component in Flex .??? ^..^
I'm eager to know about as it comes in handy in the nearest future!!!
You can, but I don't see the point of storing your images in a DB.
Simplest way to get it into an Image is to load the blob, convert to a ByteArray which you can set as the source of of said Image.
If you override HttpService you can use it to receive binary data. If you don't want to override HttpService you have the option of encoding you binary data in base64 before sending it.
But if have the option to store the images in a directory on the server and just send links to the client - that would be a better solution.

ASP/VB Byte arrays, iframes, parents, children and variables

I have an aspx page which houses an iframe. When a button is clicked, a WCF is called to produce a PDF which is read into a byte array. I was storing the byte array in a Globals.vb file like this:
Public Shared PDF_Data as Byte()
The global was loaded from the parent aspx page like this:
PDF_Data = MyWCF.Create_PDF_File(SomeVariable)
After that, the iFrame's src was set to a blank aspx page, which had the following code in the page_load event:
'Write the PDF binary data to the screen (viewer)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.BinaryWrite(Globals.PDF_Data.ToArray)
However, realizing that this application will have several users who will get different PDF documents, I have learned that this is not the way to go. My shared variable would be accessible to all users, a big no-no.
However, I am stumped as to how I'm going to store the byte array and make it available to a child aspx page from it's parent.
Any ideas would be greatly appreciated!
Thanks,
Jason
The shared variable is definitely not the way to go. I took over a project that used that technique and there were slews of issues with one user getting another users data. You should either use Session, which in itself can be an issue.
One suggestion that I've used saving the byte data to a database with a key and passing that key to the iframe within the URL with the query string. In that case, you should have a way to clear out the past records from the db before it takes up too much space. Depending on if this PDF document is supposed to be secure, this will open up so that the PDF would be accessible by people fiddling with the query string.
Another suggestion, passing it as B64 encoded POST data. Those are a couple suggestions.

Opening an image in vb to send into a database - Asp.Net

I'm currently patching an asp.net program where I need to be able to send an image to an SQL Server 2005 DB. It works fine when I use the asp:fileupload control, but the trick is that when the user deletes the image, I'm supposed to replace it with an image from the server saying "empty", in code-behind.
I know how to open, use and save text files in vb, but I can't find any information anywhere on how to open an image / binary file in a similar manner so that I can use it as an sql-parameter on the update query.
Below is an example of how easy it is to use a file from the fileupload control.
Dim t_id As Integer = Convert.ToInt32(Request.QueryString("id"))
open()
Dim picture As New SqlParameter("#picture", pictureFileUpload.FileBytes)
Dim id As New SqlParameter("#id", t_id)
myCommand = New SqlCommand("spChangeImage")
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Connection = conn
myCommand.Parameters.Add(picture)
myCommand.Parameters.Add(id)
myCommand.ExecuteNonQuery()
close()
Now I need a way to open an image file and set it as a parameter in a similar manner, but I've no clue as to how to go about doing that. All the search results are focused on opening and viewing an image in html, I just need the binary to use it in the query. I'm trying to use binaryreader but even then I've no idea how to actually map the file to begin with.
Thanks in advance for any help!
Personally, I wouldn't store this image in the database when the user deletes their value. I would set the column to null. When writing the image I would detect if the column is null, then read the file and write it to the response. If you do this, then you won't need to accumulate anything into a local buffer, you can just write each buffer to the response as it is read. You can use FileInfo.Length to determine the content length of the response.
If you insist on putting the image in the DB, you can also use FileInfo.Length to determine the size of buffer you need to hold the image. Use your BinaryReader to read this length of bytes into the buffer. The buffer then becomes your parameter to the SQL command.
this might help.

Resources