Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bitmap.Save causes Page_Load to be executed twice

Posted on 2015-01-21
2
Medium Priority
?
54 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

Technology Partners: 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

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 …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

609 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