1 |
bournival |
40 |
Imports System
|
2 |
|
|
Imports System.Collections
|
3 |
|
|
Imports System.Reflection
|
4 |
|
|
Imports System.IO
|
5 |
|
|
Imports System.Runtime.InteropServices
|
6 |
|
|
Imports System.Drawing
|
7 |
|
|
Imports System.Drawing.Imaging
|
8 |
|
|
Namespace MAGIC
|
9 |
|
|
Public Class BitmapHandler
|
10 |
|
|
Implements IDisposable
|
11 |
|
|
|
12 |
|
|
'The list of bitmap files that have been created in the Temp folder
|
13 |
|
|
Dim files As New ArrayList
|
14 |
|
|
|
15 |
|
|
Sub Dispose() Implements IDisposable.Dispose
|
16 |
|
|
CleanFiles()
|
17 |
|
|
End Sub
|
18 |
|
|
|
19 |
|
|
'Creates a file on disk from the specified bitmap resource
|
20 |
|
|
Function CreateFileFromResourceBitmap(ByVal bitmapName As String) As String
|
21 |
|
|
Dim thisAssembly As [Assembly]
|
22 |
|
|
Dim byteStream As Stream
|
23 |
|
|
Dim image As Bitmap
|
24 |
|
|
Dim filePath As String
|
25 |
|
|
|
26 |
|
|
'Initial Values
|
27 |
|
|
filePath = System.IO.Path.GetTempFileName()
|
28 |
|
|
|
29 |
|
|
'Obtain the bitmap
|
30 |
|
|
thisAssembly = System.Reflection.Assembly.GetAssembly(Me.GetType())
|
31 |
|
|
byteStream = thisAssembly.GetManifestResourceStream(bitmapName)
|
32 |
|
|
image = New Bitmap(byteStream)
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
Try
|
36 |
|
|
'Try to save the image to disk
|
37 |
|
|
image.Save(filePath)
|
38 |
|
|
CreateFileFromResourceBitmap = filePath
|
39 |
|
|
files.Add(filePath)
|
40 |
|
|
Catch e As Exception
|
41 |
|
|
'If it failed to save, return an empy path string
|
42 |
|
|
CreateFileFromResourceBitmap = ""
|
43 |
|
|
Return ""
|
44 |
|
|
Finally
|
45 |
|
|
'Clean things up
|
46 |
|
|
image.Dispose()
|
47 |
|
|
thisAssembly = Nothing
|
48 |
|
|
byteStream.Close()
|
49 |
|
|
byteStream = Nothing
|
50 |
|
|
End Try
|
51 |
|
|
End Function
|
52 |
|
|
|
53 |
|
|
'Removes the image files from your temp folder
|
54 |
|
|
Function CleanFiles() As Boolean
|
55 |
|
|
Dim curFile As String
|
56 |
|
|
|
57 |
|
|
While files.Count > 0
|
58 |
|
|
Try
|
59 |
|
|
'Try to delete the file
|
60 |
|
|
curFile = files.Item(0)
|
61 |
|
|
File.Delete(curFile)
|
62 |
|
|
files.RemoveAt(0)
|
63 |
|
|
Catch e As Exception
|
64 |
|
|
files.RemoveAt(0)
|
65 |
|
|
End Try
|
66 |
|
|
End While
|
67 |
|
|
End Function
|
68 |
|
|
End Class
|
69 |
|
|
|
70 |
|
|
End Namespace |