Showing posts with label errors. Show all posts
Showing posts with label errors. Show all posts

Saturday, March 31, 2012

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
--
<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. import namespace="system.drawing" %>
<%@dotnet.itags.org. import namespace="system.drawing.imaging" %>
<%@dotnet.itags.org. 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()
%>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
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=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1O
Q%2
fJ5V3Y%3d
and looking at the properties of the image it shows:
digifan.nl/ShowFullImage.aspx? img=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1O
Q%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-schang@.online.microsoft.com> schreef in bericht
news:VQpV0n2UEHA.3204@.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
>

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
-----------

<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. import namespace="system.drawing" %>
<%@dotnet.itags.org. import namespace="system.drawing.imaging" %>
<%@dotnet.itags.org. 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()

%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-schang@.online.microsoft.com> schreef in bericht
news:VQpV0n2UEHA.3204@.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
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

Saturday, March 24, 2012

System.Net.Webclient screen scraping: how to gracefully handle 403 (and other) errors?

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs. It
worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.

I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next ID
in the dataset rather than aborting the whole job.

My code appears below. Thank you in advance.

-KF

string strConnection;

strConnection = ConfigurationSettings.AppSettings["connwhatever"];

SqlConnection conn = new SqlConnection(strConnection);

string query = // [my query];

SqlDataAdapter a = new SqlDataAdapter(query, conn);

DataSet s = new DataSet();

a.Fill(s);

int counter = 0;

foreach (DataRow dr in s.Tables[0].Rows)

{

counter++;

System.Net.WebClient wc = new WebClient();

string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());

FileStream fstream = new FileStream(@dotnet.itags.org."c:\whateverpath\" + dr[0].ToString() +
".htm", FileMode.Create, FileAccess.Write);

StreamWriter stream = new StreamWriter(fstream);

stream.Write(strData);

stream.Close();

fstream.Close();please read chapter on try/catch

-- bruce (sqlwork.com)

kenfine@.nospam.nospam wrote:

Quote:

Originally Posted by

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs. It
worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.
>
I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next ID
in the dataset rather than aborting the whole job.
>
My code appears below. Thank you in advance.
>
-KF
>
string strConnection;
>
strConnection = ConfigurationSettings.AppSettings["connwhatever"];
>
SqlConnection conn = new SqlConnection(strConnection);
>
string query = // [my query];
>
SqlDataAdapter a = new SqlDataAdapter(query, conn);
>
DataSet s = new DataSet();
>
a.Fill(s);
>
int counter = 0;
>
foreach (DataRow dr in s.Tables[0].Rows)
>
{
>
counter++;
>
System.Net.WebClient wc = new WebClient();
>
string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());
>
FileStream fstream = new FileStream(@."c:\whateverpath\" + dr[0].ToString() +
".htm", FileMode.Create, FileAccess.Write);
>
StreamWriter stream = new StreamWriter(fstream);
>
stream.Write(strData);
>
stream.Close();
>
fstream.Close();
>
>
>
>


Understand try/catch generally. What event(s) should I be trying to catch?

Thank you,
-KF

"bruce barker" <nospam@.nospam.comwrote in message
news:%23F79bSINHHA.3288@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

please read chapter on try/catch
>
-- bruce (sqlwork.com)
>
kenfine@.nospam.nospam wrote:

Quote:

Originally Posted by

>I've written a very small ASP.NET page to scrape thousands of pages of
>content based on database IDs. It loops through a dataset to get the IDs.
>It worked well in testing but now I am getting an annoying 403 error that
>causes the script to abort halfway through my download.
>>
>I am wondering if there is a way in ASP.NET to have my code ignore 403
>errors and other network errors, catch the error, and iterate to the next
>ID in the dataset rather than aborting the whole job.
>>
>My code appears below. Thank you in advance.
>>
>-KF
>>
>string strConnection;
>>
>strConnection = ConfigurationSettings.AppSettings["connwhatever"];
>>
>SqlConnection conn = new SqlConnection(strConnection);
>>
>string query = // [my query];
>>
>SqlDataAdapter a = new SqlDataAdapter(query, conn);
>>
>DataSet s = new DataSet();
>>
>a.Fill(s);
>>
>int counter = 0;
>>
>foreach (DataRow dr in s.Tables[0].Rows)
>>
>{
>>
>counter++;
>>
>System.Net.WebClient wc = new WebClient();
>>
>string strData =
>wc.DownloadString("http://whatever.org/article.asp?articleid=" +
>dr[0].ToString());
>>
>FileStream fstream = new FileStream(@."c:\whateverpath\" +
>dr[0].ToString() + ".htm", FileMode.Create, FileAccess.Write);
>>
>StreamWriter stream = new StreamWriter(fstream);
>>
>stream.Write(strData);
>>
>stream.Close();
>>
>fstream.Close();
>>
>>
>>


Hello KF,

Based on your description, you're using the webclient class to request many
web pages programmatically in ASP.NET page code. However, since some page
may raise some exception, your client loop code in ASP.NET page break,
correct?

As for the 403 error, it is normally caused by the security authorization
checking at server-side fails. I'm not sure whether there is any other
particular scenario here, however, if what you want is simply captuer and
ignore such error and continue the loop, you can just add a try catch block
around your webclient class's downloadXXX method call and if any exception
captured you can simply ignore it and skip the current loop. e.g.

=======================
foreach (DataRow dr in s.Tables[0].Rows)

{

counter++;

System.Net.WebClient wc = new WebClient();

try
{

string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());

}catch(Exception ex)
{
//ignore and continue the loop
}

...........................

}
=========================

Does this work for your scenario?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
For webclient or HttpWebRequest, it normally will throw a
System.Net.WebException, however, any exception can be handled by the super
class "Exception". So you can use either

try
{
}catch(Exception)
{

}

or

try
{
}catch(WebException)
{

}

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
You can specify error documents for specific error-codes.

In your web.config, add the following entries in <system.websection:

<customErrors>
<error statusCode="403" redirect="403.aspx"/>
</customErrors>

Note that generally, 403 would be given by the web server and not be the
ASP.Net engine. At times, when the authentication fails, 403 may be returned
by an IHttpModule - like the authentication modules (NTML, Kerberos, Digest
etc).

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------

<kenfine@.nospam.nospamwrote in message
news:%23SginPGNHHA.4916@.TK2MSFTNGP06.phx.gbl...

Quote:

Originally Posted by

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs.
It worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.
>
I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next
ID in the dataset rather than aborting the whole job.
>
My code appears below. Thank you in advance.


This worked great for my scenario. Thanks very much to everyone for the
timely assistance.

-KF

"Steven Cheng[MSFT]" <stcheng@.online.microsoft.comwrote in message
news:FYKH6FJNHHA.2024@.TK2MSFTNGHUB02.phx.gbl...

Quote:

Originally Posted by

Hello KF,
>
Based on your description, you're using the webclient class to request
many
web pages programmatically in ASP.NET page code. However, since some page
may raise some exception, your client loop code in ASP.NET page break,
correct?
>
As for the 403 error, it is normally caused by the security authorization
checking at server-side fails. I'm not sure whether there is any other
particular scenario here, however, if what you want is simply captuer and
ignore such error and continue the loop, you can just add a try catch
block
around your webclient class's downloadXXX method call and if any exception
captured you can simply ignore it and skip the current loop. e.g.
>
=======================
foreach (DataRow dr in s.Tables[0].Rows)
>
{
>
counter++;
>
System.Net.WebClient wc = new WebClient();
>
try
{
>
string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());
>
}catch(Exception ex)
{
//ignore and continue the loop
}
>
...........................
>
}
=========================
>
Does this work for your scenario?
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
>
==================================================
>
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.
>
>
>
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.
>
==================================================
>
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>