?
Solved

Bitmap.Save causes Page_Load to be executed twice

Posted on 2015-01-21
2
Medium Priority
?
51 Views
Last Modified: 2016-06-23
Executing "Bitmap.Save" causes "Page_Load" to execute twice. I have done a lot of research online and have yet to find a solution to the problem. I have tried everything recommended by online articles and nothing has worked. Here is what I have done so far.

1.) Set "AutoEventWireup=false" and ensure only one handle on "Page_Load"
2.) Removed all handles on "Page_Load" and set "AutoEventWireup=true"
3.) Attempted to use "IsPostBack"; however, that is always "False" so I know nothing is being posted back

Below is a very simple test case that I came up with to reproduce the problem. Executing this code you will see in the log file that "Level 1a" appears twice. If you comment out the "Bitmap.Save" call, then the log will only show it once.

'TestPageLoad.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TestPageLoad.aspx.vb" Inherits="TestPageLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


'TestPageLoad.aspx.vb

Imports System.Drawing.Image
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web
Imports Microsoft.VisualBasic
Partial Class TestPageLoad
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

	WriteLog("Level 1a")

	Dim oBitmap As Bitmap = New Bitmap(1, 1)
	Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)

	oGraphic.DrawLine(New Pen(Color.Red), 0, 0, 1, 1)

	Response.ContentType = "image/jpeg"

	oBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
 
    End Sub

    Private Shared Sub WriteLog(ByVal Message As String)
	Dim file_name As String = "D:\debug.log"
	Dim objWriter As New System.IO.StreamWriter(file_name, True)

	objWriter.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ": " + Message)

	objWriter.Close()
    End Sub

End Class

Open in new window

0
Comment
Question by:Bizzuka IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40562731
Try clearing the response first:

...

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

    oBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
 
End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question