473,835 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetThumbnailIma ge
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.WriteF ile
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 14278
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******** ******@TK2MSFTN GP11.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 GetThumbnailIma ge 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.WriteF ile 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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlCo nnection _
("Server=localh ost;" & "DataBase=North wind;" & _
"Integrated Security=SSPI")
Dim cmd As New SqlClient.SqlCo mmand("SELECT FileName, PictureID
FROM Picture", conn)
Dim da As New SqlClient.SqlDa taAdapter(cmd)
Dim dsPictures As New DataSet
da.Fill(dsPictu res)
Me.Image1.Visib le = False
ListBox1.AutoPo stBack = True
Try
ListBox1.DataSo urce = dsPictures.Tabl es(0)
ListBox1.DataTe xtField = "FileName"
ListBox1.DataVa lueField = "PictureID"
ListBox1.DataBi nd()
Catch sqlExc As SqlClient.SqlEx ception
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_Select edIndexChanged( ByVal sender _
As System.Object, ByVal e As System.EventArg s) Handles
ListBox1.Select edIndexChanged
Session.Item("i mg") = ListBox1.Select edItem.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.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=localhost;" & _
"DataBase=North wind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Picture FROM Picture WHERE (PictureID = {0})",
CInt(Session.It em("img")))
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Response.Binary Write(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

GetThumbnailIma ge
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.WriteF ile
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.GetThumbn ailImageAbort myCallback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
System.Drawing. Image img = System.Drawing. Image.FromFile( @"c:\img.jpg ");

// Then make the thumbnail
img = img.GetThumbnai lImage(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.Ha ndle;

// Finally write this.
Response.WriteF ile(FileHandle, 0, MyFileStream.Le ngth);

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******** ******@TK2MSFTN GP11.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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlCo nnection _
("Server=localh ost;" & "DataBase=North wind;" & _
"Integrated Security=SSPI")
Dim cmd As New SqlClient.SqlCo mmand("SELECT FileName, PictureID FROM Picture", conn)
Dim da As New SqlClient.SqlDa taAdapter(cmd)
Dim dsPictures As New DataSet
da.Fill(dsPictu res)
Me.Image1.Visib le = False
ListBox1.AutoPo stBack = True
Try
ListBox1.DataSo urce = dsPictures.Tabl es(0)
ListBox1.DataTe xtField = "FileName"
ListBox1.DataVa lueField = "PictureID"
ListBox1.DataBi nd()
Catch sqlExc As SqlClient.SqlEx ception
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_Select edIndexChanged( ByVal sender _
As System.Object, ByVal e As System.EventArg s) Handles
ListBox1.Select edIndexChanged
Session.Item("i mg") = ListBox1.Select edItem.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.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=localhost;" & _
"DataBase=North wind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Picture FROM Picture WHERE (PictureID = {0})", CInt(Session.It em("img")))
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Response.Binary Write(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

GetThumbnailIma ge
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.WriteF ile
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.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=localhost;" & _
"DataBase=North wind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Picture FROM Picture WHERE (PictureID = {0})",
CInt(Session.It em("img")))
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item ("Picture"), Byte()))
Dim ms1 As New System.IO.Memor yStream(arrImag e)
Dim origimage As System.drawing. Image
origimage = System.Drawing. Image.FromStrea m(ms1)
Dim PThumbnail As System.drawing. Image
PThumbnail = origimage.GetTh umbnailImage(10 0, 100, Nothing, New
IntPtr)
Dim ms2 As New System.IO.Memor yStream
PThumbnail.Save (ms2, Imaging.ImageFo rmat.Bmp)
arrImage = ms2.GetBuffer
Response.Binary Write(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******** ******@TK2MSFTN GP11.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.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=localhost;" & _
"DataBase=North wind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Picture FROM Picture WHERE (PictureID = {0})", CInt(Session.It em("img")))
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item ("Picture"), Byte()))
Dim ms1 As New System.IO.Memor yStream(arrImag e)
Dim origimage As System.drawing. Image
origimage = System.Drawing. Image.FromStrea m(ms1)
Dim PThumbnail As System.drawing. Image
PThumbnail = origimage.GetTh umbnailImage(10 0, 100, Nothing, New
IntPtr)
Dim ms2 As New System.IO.Memor yStream
PThumbnail.Save (ms2, Imaging.ImageFo rmat.Bmp)
arrImage = ms2.GetBuffer
Response.Binary Write(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.Conten tType = "image/jpeg" before you output the stream.

mynewimage.Save (Response.Outpu tStream, ImageFormat.Jpe g)

"Toby Mathews" <NO***********@ NOSPAMcix.co.uk > wrote in message
news:uO******** ******@TK2MSFTN GP11.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 GetThumbnailIma ge 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.WriteF ile 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
18097
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 wich iTextSharp.text.Image.getInstance(...) signature I am using : - using code 1 below, the conversion is fast enough but the resulting PDF file is too big (1 817ko sample Tiff file is converted in less than 30 seconds to a 2 764ko PDF file) -...
1
50644
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 convert the following code so that the Pic2 can accept the Image from the stream where it wants Picture property not and Image property. Here is a snippet, I'm trying to avoid writing to disk.: Image i = Pic1.Image;
3
3638
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 application, while I am doing for web. I can only find Image from web forms control and HTML control. This may be the root cause of my problem. For read button, I converted his VB to the C#. But the compiler complains:
4
3308
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
3
20125
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 "My Documents" folder or subfolders. If the file lives anywhere else on the hard drive, I have no problems. What could be going on here?
4
2423
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 for me to use ? Loads of karma in it for you :) <%@ Import Namespace="System.Collections" %>
7
3404
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
3885
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 a web service hosted in IIS on Windows XP. Typically, I have to run IISRESET from the Command Line to be able to rename the file. My work around is to use a different method: Dim fs As FileStream = New FileStream(pictureFile, FileMode.Open,...
7
472
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, 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...
0
10812
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
10523
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
10235
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6966
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();...
0
5638
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
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
3995
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3089
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.