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

System.Drawing: Error when using a .bmp or .gif file but not when .jpg

I each time get errors when I use an .bmp or ,.gif file as source but
whenever I use a .jpg file it works perfect.
Why is that? and how to fix it?

I almost have no knowledge about the system.drawing and so I am in the dark
about this.

This is my code
----------------------------------

<%@ Page Language="vb" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<%

' initialise objects
dim strFilename as string
dim b as New System.Drawing.Bitmap(server.mappath("test.jpg"))
dim g as graphics = graphics.fromimage(b)
g.smoothingMode = smoothingMode.antiAlias
dim stringFormat As New StringFormat()
g.drawString("The Magic Word", New font("arial",3),systembrushes.windowtext,
New pointF(2,2), stringFormat)

' Set the content type
response.contenttype="image/jpeg"

' send the image to the viewer
b.save(response.outputstream, b.rawformat)

' tidy up
b.dispose()

%>
Nov 18 '05 #1
2 1472
Hi Richard,

As for the "A Graphics object cannot be created from an image that has an
indexed pixel format" error, it is caused by the Graphic object can't been
created from some certain image format which contains indexed pixel. I've
done some tests and one means is load the image and convert it into jpeg
format first and then create Graphich object to manipulate it. For example,
here is my test code:
==================================================
Private Sub Output_Image(ByVal path As String)
Try

Dim img As Image = Image.FromFile(path)
Dim stream As New System.IO.MemoryStream
img.Save(stream, ImageFormat.Jpeg)
Dim imgNew As Image = Image.FromStream(stream)

Dim g As Graphics = Graphics.FromImage(imgNew)

g.DrawString("MS Test", New Font("Verdana", 20,
FontStyle.Bold), _
New SolidBrush(Color.Beige), 0, 0)

Response.Clear()
Response.ContentType = "Image/jpeg"

imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.End()
img.Dispose()
stream.Close()
imgNew.Dispose()

Catch ex As Exception
Response.Write("<br>" + ex.Message)
End Try

End Sub
=================================================

Please have a try to see whether it works for you. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #2
thx a lot, this really helped me out.

I got no errors and with some thinking I made sure ppl can't link to my
files. (put a http://www. infront of url to view)

digifan.nl/ShowGalleryImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK 7mbHBTzFQQ1OQ%2
fJ5V3Y%3d

and looking at the properties of the image it shows:

digifan.nl/ShowFullImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK7mb HBTzFQQ1OQ%2fJ5
V3Y%3d

which is only the image.

Again thx, i am going to read a bit some more about the system.drawing and
what it al can do but now i have an understanding of
the general thing.

Richard

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> schreef in bericht
news:VQ**************@cpmsftngxa10.phx.gbl...
Hi Richard,

As for the "A Graphics object cannot be created from an image that has an
indexed pixel format" error, it is caused by the Graphic object can't been
created from some certain image format which contains indexed pixel. I've
done some tests and one means is load the image and convert it into jpeg
format first and then create Graphich object to manipulate it. For example, here is my test code:
==================================================
Private Sub Output_Image(ByVal path As String)
Try

Dim img As Image = Image.FromFile(path)
Dim stream As New System.IO.MemoryStream
img.Save(stream, ImageFormat.Jpeg)
Dim imgNew As Image = Image.FromStream(stream)

Dim g As Graphics = Graphics.FromImage(imgNew)

g.DrawString("MS Test", New Font("Verdana", 20,
FontStyle.Bold), _
New SolidBrush(Color.Beige), 0, 0)

Response.Clear()
Response.ContentType = "Image/jpeg"

imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.End()
img.Dispose()
stream.Close()
imgNew.Dispose()

Catch ex As Exception
Response.Write("<br>" + ex.Message)
End Try

End Sub
=================================================

Please have a try to see whether it works for you. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #3

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

Similar topics

2
by: James Coburn's Grey Helmet Hair | last post by:
When I try to compile my GTK# app, the compiler says: jbailo@linux:~/mono> mcs buttons.cs -r gtk-sharp.dll -r glib-sharp.dll -r -o buttons.exe error CS2001: Source file 'buttons.exe' could not...
3
by: Michael Bøcker-Larsen | last post by:
Hi I'v been stuck on this problem for ages now. I have found that I'm not the only one with this problem, by looking through the different newsgroups. Hope you can help me! I know there is a...
3
by: Sarah | last post by:
I have a function residing in a module that is doing some file copy and database query operations. This function is called from a regular form under a System.Windows.Forms.Timer control. The...
7
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
1
by: Ratz | last post by:
Hello everyone! I'm new to this Forum! I've spent about 56 hours trying to solve a .NET VB Runtime error while Using a program.Ive contacted the developer of the program and he could not figure out...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
2
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.