473,382 Members | 1,752 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,382 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 12452
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...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.