FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpDevelop/samples/EmbeddedImageAddIn/ImageCache.cs at master · icsharpcode/SharpDevelop · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Oct 16, 2020. It is now read-only.
icsharpcode
/
SharpDevelop
Public archive
Notifications
You must be signed in to change notification settings
Fork
808
Star
2.2k
Code
Issues
295
Pull requests
21
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpDevelop
/
samples
/
EmbeddedImageAddIn
/
ImageCache.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (53 loc) · 1.54 KB
Breadcrumbs
SharpDevelop
/
samples
/
EmbeddedImageAddIn
/
ImageCache.cs
Copy path
File metadata and controls
58 lines (53 loc) · 1.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Windows
.
Media
;
using
System
.
Windows
.
Media
.
Imaging
;
using
ICSharpCode
.
Core
;
namespace
EmbeddedImageAddIn
{
public
static
class
ImageCache
{
static
readonly
Dictionary
<
FileName
,
WeakReference
>
imageCache
=
new
Dictionary
<
FileName
,
WeakReference
>
(
)
;
public
static
ImageSource
GetImage
(
FileName
fileName
)
{
lock
(
imageCache
)
{
WeakReference
wr
;
ImageSource
image
;
// retrieve image from cache, if possible
if
(
imageCache
.
TryGetValue
(
fileName
,
out
wr
)
)
{
image
=
(
ImageSource
)
wr
.
Target
;
if
(
image
!=
null
)
return
image
;
}
// load image:
image
=
LoadBitmap
(
fileName
)
;
if
(
image
!=
null
)
imageCache
[
fileName
]
=
new
WeakReference
(
image
)
;
// clean up cache:
List
<
FileName
>
entriesToRemove
=
(
from
p
in
imageCache
where
!
p
.
Value
.
IsAlive
select
p
.
Key
)
.
ToList
(
)
;
foreach
(
var
entry
in
entriesToRemove
)
imageCache
.
Remove
(
entry
)
;
return
image
;
}
}
static
BitmapImage
LoadBitmap
(
string
fileName
)
{
try
{
if
(
File
.
Exists
(
fileName
)
)
{
BitmapImage
bitmap
=
new
BitmapImage
(
new
Uri
(
fileName
)
)
;
bitmap
.
Freeze
(
)
;
return
bitmap
;
}
}
catch
(
ArgumentException
)
{
// invalid filename syntax
}
catch
(
IOException
)
{
// other IO error
}
return
null
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL