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

Returning a Bitmap Object from a WebMethod

I am new to Web Services and .NET development, and I have a question.

I am writing a service that will create a bitmap image and return it to the
client. First, I wrote a method that looked like this:

public System.Drawing.Bitmap CreateImage() {...}

When I tried to create the web reference in my client program, I got an
error message stating that the System.Drawing.Bitmap object could not be
serialized because it does not have a public default constructor. Then I
changed the method to something like this:

public bool CreateImage(ref System.Drawing.Bitmap bitmap) {...}

Unfortunately, this resulted in the same error message.

The help for the Bitmap class indicates that it has been declared with the
[serializable] attribute, which leads me to believe that what I am doing
should work. Alas, it does not.

Does anyone know what I need to do to make this work, or have a sample
program that does something similar?

Thanks,

Bob

Nov 23 '05 #1
6 12447
rlcavebmg wrote:
I am new to Web Services and .NET development, and I have a question.

I am writing a service that will create a bitmap image and return it
to the client. First, I wrote a method that looked like this:

... snipped for brevity ...

Does anyone know what I need to do to make this work, or have a sample
program that does something similar?


Yeah, you can't just return a Bitmap for a bunch of reasons. What you'll
need to do is get the bytes of the bitmap and return them using Base64 encoding.
It might look a little something like this:

<codeSnippet language="C#">
[WebMethod]
[return:XmlElement("imageData", DataType="base64Binary")]
public byte[] CreateImage()
{
using(Bitmap image = new Bitmap(100, 100))
using(Graphics imageGraphics = Graphics.FromImage(image))
{
imageGraphics.FillRectangle(Brushes.Red, 0, 0, image.Width, image.Height);
imageGraphics.DrawRectangle(Pens.Blue, 0, 0, image.Width, image.Height);

using(MemoryStream stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Png);

stream.Flush();

return stream.ToArray();
}
}
}
</codeSnippet>

As you can see I've decorated the result of the method with an XmlElementAttribute
with the DataType base64Binary which instructs the XmlSerializer to serialize
the byte array into an element named "imageData" whose contents is base64
encoded. This is not the most efficient way to acheive this since you need
to encode/decode the bytes, but is probably the most cross platform compatible.
If you're using WSE on both ends you might want to consider using DIME, which
will allow you to basically send just the raw bytes, but that's something
I'm not all that experienced with personally.

HTH,
Drew

Nov 23 '05 #2

"Drew Marsh" wrote:
...snipped for brevity...



Drew,

Thanks. That was very helpful.

On the client side, it looks like I ought to be able to do something like
the following snippet to get the data into a Bitmap object:

byte[] byteArray = CreateImage();
MemoryStream stream = new MemoryStream(byteArray);
Bitmap bitmap = new Bitmap(stream);

Is this correct?

Cheers,

Bob
Nov 23 '05 #3
rlcavebmg wrote:
On the client side, it looks like I ought to be able to do something
like the following snippet to get the data into a Bitmap object:


Duh, yeah, I shoulda just included my test client side code for you. Here,
this was on a test winforms app as part of a button click. It just gets the
image and draws it on the form:

<codeSnippet language="C#">
Service1 service = new Service1();

byte[] imageBytes = service.CreateImage();

using(MemoryStream stream = new MemoryStream(imageBytes, false))
using(Image image = Image.FromStream(stream))
{
this.CreateGraphics().DrawImage(image, 0, 0);
}
</codeSnippet>

HTH,
Dre

Nov 23 '05 #4
Hello Drew,
Ideally you should try using Dime attachments.
http://msdn.microsoft.com/msdnmag/is...E/default.aspx
HTH
Regards,
Dilip Krishnan
MCAD, MCSD.net
dkrishnan at geniant dot com
http://www.geniant.com
rlcavebmg wrote:
On the client side, it looks like I ought to be able to do something
like the following snippet to get the data into a Bitmap object:

Duh, yeah, I shoulda just included my test client side code for you.
Here, this was on a test winforms app as part of a button click. It
just gets the image and draws it on the form:

<codeSnippet language="C#">
Service1 service = new Service1();
byte[] imageBytes = service.CreateImage();

using(MemoryStream stream = new MemoryStream(imageBytes, false))
using(Image image = Image.FromStream(stream))
{
this.CreateGraphics().DrawImage(image, 0, 0);
}
</codeSnippet>

HTH,
Drew

Nov 23 '05 #5
Could you please write an example of a web application doing the same job..
Because I am dealing with the same issue , tranferring byte[] from the
WebService, but I couldnt find any method to transform this data into a
WebControls.Image object.
Thanks a lot ,
Bahadir Cambel

"Drew Marsh" wrote:
rlcavebmg wrote:
On the client side, it looks like I ought to be able to do something
like the following snippet to get the data into a Bitmap object:


Duh, yeah, I shoulda just included my test client side code for you. Here,
this was on a test winforms app as part of a button click. It just gets the
image and draws it on the form:

<codeSnippet language="C#">
Service1 service = new Service1();

byte[] imageBytes = service.CreateImage();

using(MemoryStream stream = new MemoryStream(imageBytes, false))
using(Image image = Image.FromStream(stream))
{
this.CreateGraphics().DrawImage(image, 0, 0);
}
</codeSnippet>

HTH,
Drew

Nov 23 '05 #6
Hi,

I saw this question by accident, does this sample I once made fit the
problem?
As database is a very easy dataset file used on the serverside.

\\\needs a picturebox and 4 buttons on a windowform
Private ds As New DataSet
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Reading a picture from disk and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray to disk
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a webservice dataset
Dim ws As New localhost.DataBaseUpdate
ws.SetDataset(abyt)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a webservice dataset
Dim ws As New localhost.DataBaseUpdate
abyt = ws.GetByte
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
End Class
////
\\\\Webservice
<WebMethod()> _
Public Function GetByte() As Byte()
Dim ds As New DataSet
ds.ReadXml("C:\wsblob.xml")
Return CType(ds.Tables(0).Rows(0)(0), Byte())
End Function
<WebMethod()> _
Public Sub SetDataset(ByVal abyte As Byte())
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType = GetType(System.Byte())
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyte
ds.WriteXml("C:\wsblob.xml", XmlWriteMode.WriteSchema)
End Sub
////

I hope this helps a little bit?

Cor
"rlcavebmg" <rl*******@discussions.microsoft.com>
I am new to Web Services and .NET development, and I have a question.

I am writing a service that will create a bitmap image and return it to
the
client. First, I wrote a method that looked like this:

public System.Drawing.Bitmap CreateImage() {...}

When I tried to create the web reference in my client program, I got an
error message stating that the System.Drawing.Bitmap object could not be
serialized because it does not have a public default constructor. Then I
changed the method to something like this:

public bool CreateImage(ref System.Drawing.Bitmap bitmap) {...}

Unfortunately, this resulted in the same error message.

The help for the Bitmap class indicates that it has been declared with the
[serializable] attribute, which leads me to believe that what I am doing
should work. Alas, it does not.

Does anyone know what I need to do to make this work, or have a sample
program that does something similar?

Thanks,

Bob

Nov 23 '05 #7

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

Similar topics

2
by: Boffo Jinko | last post by:
This should be obvious, but I can't figure it out... I have the following web service: <WebMethod (Description:="Returns a dataset of all people from the database.")> _ Public Function...
1
by: Knecke | last post by:
Hi all. I have a problem with returning a custom Result object with webservice. The classes i use is described below (some fields and properties is removed) public class Result { int...
5
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
7
by: Ohad Young | last post by:
Hi, I was wondering if it is possible to code a webservice that returns a picture? If so, How? Thanks in advance, Ohad
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
3
by: Khurram | last post by:
Hi, Firstly, I will apologise now if I have posted in the wrong discussion group. Please let me know if I have for future reference. Below is the code to a WebMethod that is querying an Access...
0
by: TMesh | last post by:
Hello Is it possible to return a Crystal ReportDocument from a WebService? I keep getting the following error: Exception: System.Web.Services.Protocols.SoapException: Server was unable to...
2
by: Asim Qazi | last post by:
Hi All public class MyResponse { public bool m_bStatus; public string m_szErrorCode; public string m_szMessage; }
1
by: Ronchese | last post by:
Helllo. I need to return a XML from my WebService, but I'm not getting the result as a XML. I mean, instead of receiveing a tag (<xyy></xyz>), I'm receiving it encoded (with &lt; or &gt;). There is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.