Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Very simple program. Basically saves and image to disk and displays it on the web page. Here's the code.
XML
<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <p>
        <asp:Image ID="Image1" runat="server" Height="131px" Width="127px" />
    </p>
    </asp:Content>

- - - - - This is the codeBehind - - - - - - -
VB
Option Strict On
Imports System.DirectoryServices
Imports System.IO
Imports System.Drawing
Public Class _Default
    Inherits System.Web.UI.Page
    Dim sSavePath As String = Nothing
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Using Root As New DirectoryEntry 'Establish connection to current loged on users Active Directory
            Using Searcher As New DirectorySearcher(Root) 'Start at the top                
                Searcher.Filter = "(&(objectCategory=user)(cn=Robin.kirchhof))"
                Searcher.SearchScope = SearchScope.Subtree 'Start at the top and keep drilling down
                Searcher.PropertiesToLoad.Add("thumbnailPhoto") 'Load picture
                Dim User = Searcher.FindOne 'Users contains our searh results

                If User.Properties.Contains("thumbnailPhoto") Then '<--This makes sure the property actually exists and has a value
                    Dim bytBLOBData() As Byte = CType((User.Properties("thumbnailPhoto")(0)), Byte()) '<-- we need to use 0 here because this attribute only has one value
                    Using stmBLOBData As New MemoryStream(bytBLOBData) 'Create new memory stream.
                        Dim TempImg As System.Drawing.Image
                        TempImg = System.Drawing.Image.FromStream(stmBLOBData) 'Load image from stream
                        TempImg.Save(Server.MapPath("~/Images") + "\TempImg.jpg") 'save image to disk
                        TempImg.Dispose()
                        Image1.ImageUrl = (Server.MapPath("~/Images") + "\TempImg.jpg")
                        Image1.AlternateText = "Users Picture"
                    End Using
                Else
                    Image1 = Nothing
                    Image1.AlternateText = "NoPicture"
                End If
            End Using
        End Using
    End Sub
End Class


The problem is the picture is not displayed. The blob data memory stream is being saved to the image folder as a jpeg. I can browse to it and open it in explorer. but the Image1.imageUrl assignment dosen't seem to be working. Any idea what I'm doing wrong?
Posted

try this :

Image1.ImageUrl = (Server.MapPath("~/Images/TempImg.jpg")
 
Share this answer
 
Comments
Robert Kirchhof 15-Nov-11 16:34pm    
Nope, no difference, but thank you for trying.
Apparently I needed the Server.MapPath only for the write operation. The code below works! My thanks to Mhamad for putting me on the right track.

VB
TempImg = System.Drawing.Image.FromStream(stmBLOBData) 'Load image from stream
TempImg.Save(Server.MapPath("~/Images") + "\TempImg.jpg") 'save image to disk
TempImg.Dispose()
image1.ImageUrl = ("~/Images/TempImg.jpg")
image1.AlternateText = "Users Picture"
 
Share this answer
 
I spoke too soon. Now it works great in the IDE (Visual Studio 2010) but when I publish it to my local webserver and browse to it it crashes with the following error.

Server Error in '/WebSite' Application.
--------------------------------------------------------------------------------
A generic error occurred in GDI+. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
Stack Trace: 

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +800473
   WebApplication3._Default.Button1_Click(Object sender, EventArgs e) in C:\Users\Robert.Kirchhof.000\Documents\Visual Studio 2010\Projects\WebApplication3\WebApplication3\Default.aspx.vb:23
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
 

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900