關於VS.Net應用的圖示提取方法

智慧油氣田ing發表於2020-12-09

.Net的資原始檔

        VS.Net 支援三種檔案型別的resource:.txt、.resx、.resources。

system.resources 名字空間支援三種資原始檔:

    • txt 檔案,只能有字串資源。因為不能被嵌入到 assembly 中,所以非常容易暴露,被客戶修改。最大缺點是僅支援字串資源,所以不推薦使用。
    • .resx 檔案,由 xml 組成,能加入所有資源,包括二進位制。同樣不能被嵌入到 assembly 中。在 system.resources 名字空間中有專用讀寫的類。vs.net 建立這種檔案然後將其轉成 .resources 檔案並根據設定將其嵌入到 assembly 中。
    • .resources 檔案,pe 格式,能加入所有資源。唯一能被嵌入到 assembly 的檔案,在 system.resources 名字空間中有專用讀寫的類。
  1. 把resx檔案通過vs加入到.net工程中後,編譯完成時,它會被轉化為resources檔案並embed到assembly中。
  2. System.resources.resourceManager負責讀取.net assembly中嵌入的resources檔案中的資源資訊。
  3. 三種檔案型別,必須要轉為resources檔案,才能被resourcemanager讀取(顯然的)。ResGen.exe主要完成此項工作。
  4. C++/CLI, vb.net, c#,大家都可以用resx檔案是自然的。只不過在Vs裡新增resx檔案時,c#工程要支援的更完備些:直接替你封裝好了對resourcemanager的使用。
  5. 推薦工具
    • resgen.exe:sdk 中的工具,專門用來做資原始檔型別之間的轉換。支援 .txt <-> .resx <-> .resources 之間的轉換。
    • resourcer:專門用來建立資原始檔,簡單易用,支援 .resx 和 .resources 檔案格式。
    • .net reflector:用來瀏覽 assembly。如果你不確定一個資原始檔的完整名時能用這個工具在 assembly 中檢視。

ResX資原始檔中圖示的提取

    ResX檔案中,是將所有資源(包括二進位制)都寫入到XML檔案中,每個圖示不再以獨立檔案的形式存在。在程式設計工程中,尤其在學習、分析現有.Net應用程式過程中,需要提取現有應用程式中的圖示。基本思路是利用工具軟體,將應用程式的資原始檔提取出來(具體方法可以進行網上搜尋),生成resx或resources檔案。當生成resources檔案時,可以利用resgen.exe進行格式轉換。

    通過對resx檔案格式分析,開發了針對resx格式檔案的圖示提取工具,本工具的具體程式碼如下:

介面設計程式碼:

 

關於VS.Net應用的圖示提取方法
  1 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  2 Partial Class frmMain
  3     Inherits System.Windows.Forms.Form
  4 
  5     'Form 重寫 Dispose,以清理元件列表。
  6     <System.Diagnostics.DebuggerNonUserCode()> _
  7     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  8         Try
  9             If disposing AndAlso components IsNot Nothing Then
 10                 components.Dispose()
 11             End If
 12         Finally
 13             MyBase.Dispose(disposing)
 14         End Try
 15     End Sub
 16 
 17     'Windows 窗體設計器所必需的
 18     Private components As System.ComponentModel.IContainer
 19 
 20     '注意: 以下過程是 Windows 窗體設計器所必需的
 21     '可以使用 Windows 窗體設計器修改它。  
 22     '不要使用程式碼編輯器修改它。
 23     <System.Diagnostics.DebuggerStepThrough()> _
 24     Private Sub InitializeComponent()
 25         Me.components = New System.ComponentModel.Container()
 26         Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmMain))
 27         Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
 28         Me.ListView1 = New System.Windows.Forms.ListView()
 29         Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
 30         Me.MenuStrip1 = New System.Windows.Forms.MenuStrip()
 31         Me.FleToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
 32         Me.mnuFileOpen = New System.Windows.Forms.ToolStripMenuItem()
 33         Me.mnuFileExport = New System.Windows.Forms.ToolStripMenuItem()
 34         Me.mnuFileExit = New System.Windows.Forms.ToolStripMenuItem()
 35         Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
 36         Me.MenuStrip1.SuspendLayout()
 37         Me.SuspendLayout()
 38         '
 39         'ImageList1
 40         '
 41         Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
 42         Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
 43         Me.ImageList1.Images.SetKeyName(0, "bmp00002.bmp")
 44         Me.ImageList1.Images.SetKeyName(1, "")
 45         Me.ImageList1.Images.SetKeyName(2, "")
 46         Me.ImageList1.Images.SetKeyName(3, "")
 47         Me.ImageList1.Images.SetKeyName(4, "")
 48         Me.ImageList1.Images.SetKeyName(5, "")
 49         Me.ImageList1.Images.SetKeyName(6, "")
 50         Me.ImageList1.Images.SetKeyName(7, "")
 51         Me.ImageList1.Images.SetKeyName(8, "")
 52         Me.ImageList1.Images.SetKeyName(9, "")
 53         Me.ImageList1.Images.SetKeyName(10, "")
 54         Me.ImageList1.Images.SetKeyName(11, "")
 55         Me.ImageList1.Images.SetKeyName(12, "")
 56         Me.ImageList1.Images.SetKeyName(13, "")
 57         Me.ImageList1.Images.SetKeyName(14, "")
 58         '
 59         'ListView1
 60         '
 61         Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
 62         Me.ListView1.HideSelection = False
 63         Me.ListView1.LargeImageList = Me.ImageList1
 64         Me.ListView1.Location = New System.Drawing.Point(0, 25)
 65         Me.ListView1.Name = "ListView1"
 66         Me.ListView1.Size = New System.Drawing.Size(763, 544)
 67         Me.ListView1.SmallImageList = Me.ImageList1
 68         Me.ListView1.TabIndex = 3
 69         Me.ListView1.UseCompatibleStateImageBehavior = False
 70         Me.ListView1.View = System.Windows.Forms.View.SmallIcon
 71         '
 72         'OpenFileDialog1
 73         '
 74         Me.OpenFileDialog1.FileName = "OpenFileDialog1"
 75         '
 76         'MenuStrip1
 77         '
 78         Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.FleToolStripMenuItem})
 79         Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
 80         Me.MenuStrip1.Name = "MenuStrip1"
 81         Me.MenuStrip1.Size = New System.Drawing.Size(763, 25)
 82         Me.MenuStrip1.TabIndex = 4
 83         Me.MenuStrip1.Text = "MenuStrip1"
 84         '
 85         'FleToolStripMenuItem
 86         '
 87         Me.FleToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFileOpen, Me.mnuFileExport, Me.mnuFileExit})
 88         Me.FleToolStripMenuItem.Name = "FleToolStripMenuItem"
 89         Me.FleToolStripMenuItem.Size = New System.Drawing.Size(36, 21)
 90         Me.FleToolStripMenuItem.Text = "&Fle"
 91         '
 92         'mnuFileOpen
 93         '
 94         Me.mnuFileOpen.Name = "mnuFileOpen"
 95         Me.mnuFileOpen.Size = New System.Drawing.Size(180, 22)
 96         Me.mnuFileOpen.Text = "&Open"
 97         '
 98         'mnuFileExport
 99         '
100         Me.mnuFileExport.Name = "mnuFileExport"
101         Me.mnuFileExport.Size = New System.Drawing.Size(180, 22)
102         Me.mnuFileExport.Text = "&Export..."
103         '
104         'mnuFileExit
105         '
106         Me.mnuFileExit.Name = "mnuFileExit"
107         Me.mnuFileExit.Size = New System.Drawing.Size(180, 22)
108         Me.mnuFileExit.Text = "e&Xit"
109         '
110         'frmMain
111         '
112         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
113         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
114         Me.ClientSize = New System.Drawing.Size(763, 569)
115         Me.Controls.Add(Me.ListView1)
116         Me.Controls.Add(Me.MenuStrip1)
117         Me.MainMenuStrip = Me.MenuStrip1
118         Me.MaximizeBox = False
119         Me.MinimizeBox = False
120         Me.Name = "frmMain"
121         Me.Text = "Export Images From ImageList"
122         Me.MenuStrip1.ResumeLayout(False)
123         Me.MenuStrip1.PerformLayout()
124         Me.ResumeLayout(False)
125         Me.PerformLayout()
126 
127     End Sub
128 
129     Friend WithEvents ImageList1 As ImageList
130     Friend WithEvents ListView1 As ListView
131     Friend WithEvents OpenFileDialog1 As OpenFileDialog
132     Friend WithEvents MenuStrip1 As MenuStrip
133     Friend WithEvents FleToolStripMenuItem As ToolStripMenuItem
134     Friend WithEvents mnuFileOpen As ToolStripMenuItem
135     Friend WithEvents mnuFileExport As ToolStripMenuItem
136     Friend WithEvents mnuFileExit As ToolStripMenuItem
137     Friend WithEvents FolderBrowserDialog1 As FolderBrowserDialog
138 End Class
View Code

       程式程式碼:

關於VS.Net應用的圖示提取方法
 1 Imports System.Resources
 2 Imports Microsoft.VisualBasic.CompilerServices
 3 Public Class frmMain
 4     Private Sub mnuFileOpen_Click(sender As Object, e As EventArgs) Handles mnuFileOpen.Click
 5         Me.OpenFileDialog1 = New OpenFileDialog
 6         Me.OpenFileDialog1.DefaultExt = "resx"
 7         Me.OpenFileDialog1.AddExtension = True
 8         Me.OpenFileDialog1.Filter = ("Microsoft.Net managed Resource File") + " (*.resx)|*.resx"
 9         Me.OpenFileDialog1.FilterIndex = 1
10         If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK AndAlso Operators.CompareString(Me.OpenFileDialog1.FileName, "", False) <> 0 Then
11             filename = Me.OpenFileDialog1.FileName
12             mnuFileExport.Enabled = True
13             Dim rsxr As ResXResourceReader = New ResXResourceReader(filename)
14             Dim resxSet As New ResXResourceSet(filename)
15             Dim d As DictionaryEntry
16             Dim n As Integer = 0
17             For Each d In rsxr
18                 If Operators.CompareString(d.Value.ToString(), "System.Drawing.Bitmap", False) = 0 Then
19                     Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + "'" + d.Value.ToString() + "'")
20                     Dim image As Bitmap = CType(resxSet.GetObject(d.Key.ToString(), True), Bitmap)
21                     If image IsNot Nothing Then
22                         ImageList1.Images.Add(image)
23                         ListView1.Items.Add(New ListViewItem(n.ToString(), n))
24                         n += 1
25                     End If
26                 End If
27             Next d
28             rsxr.Close()
29         End If
30     End Sub
31     Private filename As String = ""
32     Private Sub mnuFileExport_Click(sender As Object, e As EventArgs) Handles mnuFileExport.Click
33         If Operators.CompareString(filename, "", False) <> 0 Then
34             Me.FolderBrowserDialog1.SelectedPath = "C:\"
35             Me.FolderBrowserDialog1.Description = "選擇儲存圖示的目錄"
36             If Me.FolderBrowserDialog1.ShowDialog() = DialogResult.OK AndAlso Operators.CompareString(Me.FolderBrowserDialog1.SelectedPath, "", False) <> 0 Then
37                 Dim rsxr As ResXResourceReader = New ResXResourceReader(filename)
38                 Dim resxSet As New ResXResourceSet(filename)
39                 Dim d As DictionaryEntry
40                 For Each d In rsxr
41                     If Operators.CompareString(d.Value.ToString(), "System.Drawing.Bitmap", False) = 0 Then
42                         Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + "'" + d.Value.ToString() + "'")
43                         Dim image As Bitmap = CType(resxSet.GetObject(d.Key.ToString(), True), Bitmap)
44                         If image IsNot Nothing Then
45                             image.Save(Me.FolderBrowserDialog1.SelectedPath + "\" + d.Key.ToString() + ".bmp")
46                         End If
47                     End If
48                 Next d
49                 rsxr.Close()
50             End If
51         End If
52     End Sub
53     Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
54         mnuFileExport.Enabled = (Operators.CompareString(filename, "", False) <> 0)
55     End Sub
56 End Class
View Code

 

 

   程式執行後,通過File-Open載入resx資原始檔,圖示將列在ListView控制元件中,介面如下:

 

    通過File——Export...選擇目標目錄,將圖示匯出到指定目錄。

 

相關文章