Problem with saving and retriving an Image
I have a very simple app here. It pulls a users picture from AD, saves it and then displayes it as an asp:Image. The program runs perfectly in the IDE but fails after I publish it and try to browse to it. Here's the code and the error.
Default.aspx code.
Code:
<%@ 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">
<asp:TextBox ID="TextBox1" runat="server" Width="247px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<p>
<asp:Image ID="Image1" runat="server" Height="131px" Width="127px" />
</p>
</asp:Content>
Code Behind: Default.aspx.vb
Code:
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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strCN As String = TextBox1.Text
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=" & strCN & "))"
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 = ("~/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
ERROR:
Code:
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
What am I doing wrong?
Re: Problem with saving and retriving an Image
Are you sure the account the Application Pool is running under has write access to the directory you are trying to save to?
Re: Problem with saving and retriving an Image
Very goog question. How do I even know where that is? When I browse to the foldder the wbsite is in the Images folder dosen't even exist. The path visual studio gives me for the folder (within the IDE) is "C:\Users\Robert.Kirchhof.000\Documents\Visual Studio 2010\Projects\WebApplication3\WebApplication3\Images" Do I have to create a virtual folder and point it there or should I create the Images folder under the websites root folder?
Re: Problem with saving and retriving an Image
Personally I would create an Images file right under the root in your project. Where is this deployed to? Something internal to your company or a hosting company?
Re: Problem with saving and retriving an Image
How do I determine the account the application pool is running under?
ApplicationPoolIdentity is the best I can figure.
Re: Problem with saving and retriving an Image
Quote:
Originally Posted by
RKirchhof
How do I determine the account the application pool is running under?
ApplicationPoolIdentity is the best I can figure.
That's the one. I think it defaults to the Network Service account, or you can explicitly assign it to one of your own.
Re: Problem with saving and retriving an Image
Between the path and the permissions that did it! Thanks.
Re: Problem with saving and retriving an Image
Quote:
Originally Posted by
RKirchhof
Between the path and the permissions that did it! Thanks.
No problem. If it is good to go mark the thread resolved. :D
Re: Problem with saving and retriving an Image
Quote:
Originally Posted by
SeanGrebey
No problem. If it is good to go mark the thread resolved. :D
Agreed, can you please remember to do this?
Thanks
Gary