473,320 Members | 1,853 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,320 software developers and data experts.

Convert System.Drawing.Image to FileStream

Hi,

In an ASP.Net application I want to convert open create a FileStream
object from a System.Drawing.Image - is this possible? I create an instance
of an Image object using the FromFile method, then use the GetThumbnailImage
method to create a second Image. This second one is the one I want to get a
FileStream from, so that I can then use its Handle to use Response.WriteFile
to output the thumbnail on my ASP.Net page.

Any help would be much appreciated - I realise I may not even be going
about this in the most appropriate way! Thanks in advance,

Toby Mathews
Jul 21 '05 #1
7 14136
Apologies, first line should read:

"In an ASP.Net application I want to create a FileStream..."

"Toby Mathews" <NO***********@NOSPAMcix.co.uk> wrote in message
news:uO**************@TK2MSFTNGP11.phx.gbl...
Hi,

In an ASP.Net application I want to convert open create a FileStream
object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method, then use the GetThumbnailImage method to create a second Image. This second one is the one I want to get a FileStream from, so that I can then use its Handle to use Response.WriteFile to output the thumbnail on my ASP.Net page.

Any help would be much appreciated - I realise I may not even be going
about this in the most appropriate way! Thanks in advance,

Toby Mathews

Jul 21 '05 #2
Cor
Hi Toby,

I made a sample that works. In a nice way.
It is not a thumbnail, but that you can make using the memorystream.
It is imposible to put an image from a stream direct on a webpage.
A webpage can only show an IMG.
If you want to use a thumbnail, you first needs to convert that there is a
special class for that.

I hope this is something you need.
\\\For the database the image database sample from the Resource kit is used.
\\\It needs 2 forms with on form1 a listbox, a picturebox and a label and
on form2 nothing
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim cmd As New SqlClient.SqlCommand("SELECT FileName, PictureID
FROM Picture", conn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim dsPictures As New DataSet
da.Fill(dsPictures)
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = "Database Error"
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
End Try
conn.Close()
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebApplication1/WebForm2.aspx"
End Sub
///
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})",
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
rdr.Close()
conn.Close()
End Sub
///
I hope this helps a little bit?

Cor




In an ASP.Net application I want to convert open create a FileStream
object from a System.Drawing.Image - is this possible? I create an instance
of an Image object using the FromFile method, then use the

GetThumbnailImage
method to create a second Image. This second one is the one I want to

get a
FileStream from, so that I can then use its Handle to use

Response.WriteFile
to output the thumbnail on my ASP.Net page.

Any help would be much appreciated - I realise I may not even be going
about this in the most appropriate way! Thanks in advance,

Toby Mathews


Jul 21 '05 #3
Cor
Hi Toby

I made a sample that works in a nice way. It is not a thumbnail, but that you can make using the memorystream. Skip that row above was an error I forgot to delete.
It is imposible to put an image from a stream direct on a webpage.
A webpage can only show an IMG.
If you want to use a thumbnail, you first needs to convert that there is a
special class for that.

Cor
Jul 21 '05 #4
Cor,

Thanks for the reply, I don't think that's quite what I'm after and I
can't see how to adapt it. Here's some example code to try to show what it
is I want to do (I'm using C# but I can understand your VB):

// First get my original image.
System.Drawing.Image.GetThumbnailImageAbort myCallback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\img.jpg");

// Then make the thumbnail
img = img.GetThumbnailImage(100, 100, myCallback, IntPtr.Zero);

// Then somehow make the FileStream and get a reference to the FileHandle.
FileStream MyFileStream = ... // don't know what to do here?
IntPtr FileHandle = MyFileStream.Handle;

// Finally write this.
Response.WriteFile(FileHandle, 0, MyFileStream.Length);

Does that make sense?

Thanks for your help so far, and apologies if I have missed something in
your reply.

Toby

"Cor" <no*@non.com> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Hi Toby,

I made a sample that works. In a nice way.
It is not a thumbnail, but that you can make using the memorystream.
It is imposible to put an image from a stream direct on a webpage.
A webpage can only show an IMG.
If you want to use a thumbnail, you first needs to convert that there is a
special class for that.

I hope this is something you need.
\\\For the database the image database sample from the Resource kit is used. \\\It needs 2 forms with on form1 a listbox, a picturebox and a label and
on form2 nothing
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim cmd As New SqlClient.SqlCommand("SELECT FileName, PictureID FROM Picture", conn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim dsPictures As New DataSet
da.Fill(dsPictures)
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = "Database Error"
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
End Try
conn.Close()
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebApplication1/WebForm2.aspx"
End Sub
///
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
rdr.Close()
conn.Close()
End Sub
///
I hope this helps a little bit?

Cor




In an ASP.Net application I want to convert open create a FileStream
object from a System.Drawing.Image - is this possible? I create an

instance
of an Image object using the FromFile method, then use the

GetThumbnailImage
method to create a second Image. This second one is the one I want to

get
a
FileStream from, so that I can then use its Handle to use

Response.WriteFile
to output the thumbnail on my ASP.Net page.

Any help would be much appreciated - I realise I may not even be going about this in the most appropriate way! Thanks in advance,

Toby Mathews



Jul 21 '05 #5
Cor
Hi Toby,

I changed it so it works now with a thumbnail.
That hard it cannot be to change it in C#

The changes are all on the second form.
What is the trick.
It is posible to send a bytearray to a webpage and that will create a form.
But that image is immidiatly also the page.
Not nice such a page in the left uperside of the form and no text.

The trick is to make such a page and than use that page "the URL" as the
imput for your your real image on page. That page is my form1.

I will try to make it as C# also but not today or tomorrow I think.

I hope this does help you

Cor

\\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})",
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New
IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///


Jul 21 '05 #6
Cor,

Thanks a lot, that's exactly what I needed! I was almost doing the right
thing, but I was using the FileStream rather than MemoryhStream. Thanks
again,

Toby

"Cor" <no*@non.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
Hi Toby,

I changed it so it works now with a thumbnail.
That hard it cannot be to change it in C#

The changes are all on the second form.
What is the trick.
It is posible to send a bytearray to a webpage and that will create a form. But that image is immidiatly also the page.
Not nice such a page in the left uperside of the form and no text.

The trick is to make such a page and than use that page "the URL" as the
imput for your your real image on page. That page is my form1.

I will try to make it as C# also but not today or tomorrow I think.

I hope this does help you

Cor

\\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New
IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///

Jul 21 '05 #7
I think this is what you are looking for. mynewimage is your
system.drawing.image. You may have to change the format. You also might want
to add Response.ContentType = "image/jpeg" before you output the stream.

mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)

"Toby Mathews" <NO***********@NOSPAMcix.co.uk> wrote in message
news:uO**************@TK2MSFTNGP11.phx.gbl...
Hi,

In an ASP.Net application I want to convert open create a FileStream
object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method, then use the GetThumbnailImage method to create a second Image. This second one is the one I want to get a FileStream from, so that I can then use its Handle to use Response.WriteFile to output the thumbnail on my ASP.Net page.

Any help would be much appreciated - I realise I may not even be going
about this in the most appropriate way! Thanks in advance,

Toby Mathews

Jul 21 '05 #8

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

Similar topics

0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
1
by: Fritz Switzer | last post by:
With two controls a PictureBox and another "PictureBox like" , Pic2, a control from a dll, I'm stuck on cannot implicity convert System.Drawing.Image to System.Drawing.Bitmap error. How do I...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
3
by: anastasia | last post by:
I get an out of memory exception when attempting to excecute the following code: original = System.Drawing.Image.FromFile(file.FileName,true); I ONLY get this exception when the file is in the...
4
by: Harry Hudini | last post by:
Hi, I need to run exactly this code in an asp.net file when someone uploads an image, but i dont know C# and im having real issues converting it. If anyone can, could you convert it to VB.net...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
0
by: Taiwo | last post by:
When I use the method "System.Drawing.Image.FromFile(pictureFile)" where pictureFile is the path to a valid image file, the file is locked even minutes after the statement executes. This code is in...
7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.