Solved

Bitmap.Save causes Page_Load to be executed twice

Posted on 2015-01-21
2
21 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
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now