Solved

Bitmap.Save causes Page_Load to be executed twice

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

11 Experts available now in Live!

Get 1:1 Help Now