473,698 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="syst em.drawing" %>
<%@ import namespace="syst em.drawing.imag ing" %>
<%@ import namespace="syst em.drawing.draw ing2d" %>
<%

' initialise objects
dim strFilename as string
dim b as New System.Drawing. Bitmap(server.m appath("test.jp g"))
dim g as graphics = graphics.fromim age(b)
g.smoothingMode = smoothingMode.a ntiAlias
dim stringFormat As New StringFormat()
g.drawString("T he Magic Word", New font("arial",3) ,systembrushes. windowtext,
New pointF(2,2), stringFormat)

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

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

' tidy up
b.dispose()

%>
Nov 18 '05 #1
2 1478
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(By Val path As String)
Try

Dim img As Image = Image.FromFile( path)
Dim stream As New System.IO.Memor yStream
img.Save(stream , ImageFormat.Jpe g)
Dim imgNew As Image = Image.FromStrea m(stream)

Dim g As Graphics = Graphics.FromIm age(imgNew)

g.DrawString("M S Test", New Font("Verdana", 20,
FontStyle.Bold) , _
New SolidBrush(Colo r.Beige), 0, 0)

Response.Clear( )
Response.Conten tType = "Image/jpeg"

imgNew.Save(Res ponse.OutputStr eam, ImageFormat.Jpe g)
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/ShowGalleryImag e.aspx?img=Jlz1 wB3JMO636rzX3rG MrpOK7mbHBTzFQQ 1OQ%2
fJ5V3Y%3d

and looking at the properties of the image it shows:

digifan.nl/ShowFullImage.a spx?img=Jlz1wB3 JMO636rzX3rGMrp OK7mbHBTzFQQ1OQ %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.m icrosoft.com> schreef in bericht
news:VQ******** ******@cpmsftng xa10.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(By Val path As String)
Try

Dim img As Image = Image.FromFile( path)
Dim stream As New System.IO.Memor yStream
img.Save(stream , ImageFormat.Jpe g)
Dim imgNew As Image = Image.FromStrea m(stream)

Dim g As Graphics = Graphics.FromIm age(imgNew)

g.DrawString("M S Test", New Font("Verdana", 20,
FontStyle.Bold) , _
New SolidBrush(Colo r.Beige), 0, 0)

Response.Clear( )
Response.Conten tType = "Image/jpeg"

imgNew.Save(Res ponse.OutputStr eam, ImageFormat.Jpe g)
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
391
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 be opened Compilation failed: 1 error(s), 0 warnings jbailo@linux:~/mono> mcs buttons.cs -r gtk-sharp.dll -r glib-sharp.dll -o buttons.exe buttons.cs(8) error CS0246: The namespace `System.Drawing' can not be found (missing assembly reference?)
3
12173
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 lot of information, but if you need more (e.g. the code) please write michael@mblarsen.dk. The problem is this error messages (the code is build just fine; no errors):
3
3228
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 problem is that sometimes it gives an error - even when it is doing the same exact operations. It may fail on the 20th tick event or the 100th. The file and data has no changes - so, it is doing the same operations repeatedly. I'm wondering if anyone...
7
8239
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
5163
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 why i'm getting these errors! SO i have come here for the Help from the GURUS! here is one message that pops up! See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. **************...
3
4013
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 selects? thanks...
1
7840
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 am note sure what to do because all propertiers work, except the System.Data.DataColumn.. I didn't write any code by hand and used the visual studio to set the properties..
2
2328
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 response object but how do I write it to screen? Dim objBitmap As New System.Drawing.Bitmap(400, 440) Dim objGraphics As System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(objBitmap) objGraphics.Clear(Drawing.Color.White)
2
8754
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 file, but I have been unable to get any of the overloads to work in VB.NET. I would like to store the *.ico files in a *.resx file so that users do not need anything other than the *.dll, but at the moment I am just trying to get any of the overloads...
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
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
9029
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...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
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
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.