static class TestImage { internal static void Go() { string imageName = "123.jpg"; ImageData imageData; if (ImageLibrary.TryGetImage(imageName, out imageData)) Console.WriteLine("Found It"); else Console.WriteLine("Did not find it"); } } class ImageData { internal int Width { get; private set; } internal int Height { get; private set; } internal ImageData(int width, int height) { Width = width; Height = height; } } static class ImageLibrary { static Dictionary<string, ImageData> Library = new Dictionary<string, ImageData>(); static HashSet<string> FailedLoads = new HashSet<string>(); static object ImageAddLocker = new object(); static ImageLibrary() { // if you want to preload images, do it here // otherwise take out this static constructor } internal static bool TryGetImage(string imageName, out ImageData imageData) { string name = imageName.ToUpper(); if (Library.TryGetValue(name, out imageData)) return true; if (FailedLoads.Contains(name)) return false; if (!TryFindImage(name, out imageData)) { FailedLoads.Add(name); return false; } AddImage(name, imageData); return true; } static bool TryFindImage(string name, out ImageData imageData) { string imageFullPath = Path.Combine(@"d:\tmp", name); //string imageFullPath = Path.Combine(oApp.cGraphicFat, name); if (File.Exists(imageFullPath)) { Bitmap bitmap = new System.Drawing.Bitmap(imageFullPath); imageData = new ImageData(bitmap.Width, bitmap.Height); return true; } imageData = default(ImageData); return false; } static void AddImage(string name, ImageData imageData) { lock (ImageAddLocker) { if (!Library.ContainsKey(name)) Library.Add(name, imageData); } } }>Ok, here is a revised version. This works. I had to make reference to a higher object declared at the application level and work with it along the way.
> ' Images in memory > Public oImageData As Dictionary(Of String, ImageData) = New Dictionary(Of String, ImageData) >>
>' ImageData class >Public Class ImageData > Private cExtension As String = "" > Private nHeight As Integer = 0 > Private nWidth As Integer = 0 > > Friend Property Extension() As String > Get > Return cExtension > End Get > Private Set(tcValue As String) > cExtension = tcValue > End Set > End Property > > Friend Property Height() As Integer > Get > Return nHeight > End Get > Private Set(tnValue As Integer) > nHeight = tnValue > End Set > End Property > > Friend Property Width() As Integer > Get > Return nWidth > End Get > Private Set(tnValue As Integer) > nWidth = tnValue > End Set > End Property > > Friend Sub New(tnWidth As Integer, tnHeight As Integer, tcExtension As String) > Width = tnWidth > Height = tnHeight > Extension = tcExtension > End Sub > >End Class >>
> ' If this is the Web site > If oApp.nApplicationMode = 3 Then > > ' If we have some images in memory > If oApp.oAdmin.oRow("Image").length > 0 Then > > ' Load the application into the string builder > loStringBuilderFile.LoadString(oApp.oAdmin.oRow("Image")) > > ' For each image > For lnCounter = 1 To loStringBuilderFile.nLine > > ' Get the line > If Not loStringBuilderFile.MLine(lnCounter) Then > Return False > End If > > lcImage = loStringBuilderFile.cLine > > ' If the file exists > If oApp.FileExist(oApp.cGraphicFat + lcImage) Then > > ' Get the image details making it OK to reender the HTML without flickering > loImage = New System.Drawing.Bitmap(oApp.cGraphicFat + lcImage) > lnWidth = loImage.Width > lnHeight = loImage.Height > > ' Initializaiton > lcName = Trim(lcImage) > lnLocation = oApp.At(".", lcName) > lcExtension = "" > > ' If we have found it > If lnLocation > 0 Then > > ' If this is not the last character > If lnLocation < lcName.Length Then > lcExtension = Mid(lcName, lnLocation + 1) > End If > > End If > > oApp.oImageData.Add(UCase(lcImage), New ImageData(lnWidth, lnHeight, lcExtension)) > Else > AddError(oApp.cGraphicFat + lcImage + " does not exist.") > End If > > Next > > End If > > End If >>
> ' Return True or False if an image is loaded in memory > ' expC1 Name > Public Function IsImageInMemory(ByVal tcName As String) As Boolean > Dim lcName As String = "" > Dim llFound As Boolean = False > Dim loImageData As ImageData = Nothing > > ' Initialization > lcName = UCase(Trim(tcName)) > > ' If the image is found in the dictionary > If oProcess.oApp.oImageData.TryGetValue(lcName, loImageData) Then > llFound = True > > ' Initialization > nWidth = loImageData.Width > nHeight = loImageData.Height > > End If > > Return llFound > End Function >