Solved

Bitmap.Save causes Page_Load to be executed twice

Posted on 2015-01-21
2
27 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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