/[gr2lib]/trunk/gr2lib/core/ui/typeeditors/HexEditor.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/ui/typeeditors/HexEditor.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 176 - (show annotations) (download)
Fri Jul 23 18:57:45 2010 UTC (9 years, 1 month ago) by william
File size: 4586 byte(s)
Moved Import/Export Raw Pixels ->
From: PixelByteEditor
To: HexEditor
Reason: Clean up PixelByteEditor

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using gr2lib.core.ui.rigeditor.interfaces;
10 using Be.Windows.Forms;
11 using System.IO;
12 using gr2lib.core.coretypes.implementation;
13
14 namespace gr2lib.core.ui.typeeditors
15 {
16 public partial class HexEditor : Form, IValueType<List<byte>>
17 {
18 private List<byte> _Value;
19 public List<byte> Value { get { return _Value; } set { _Value = value; } }
20
21
22 private byte[] AsBytes
23 {
24 get
25 {
26 DynamicByteProvider _provider = (DynamicByteProvider)editor.ByteProvider;
27 int _block_size = (int)_provider.Length;
28 byte[] _data = new byte[_block_size];
29 for (int i = 0; i < _provider.Length; i++)
30 {
31 _data[i] = _provider.ReadByte(i);
32 }
33 return _data;
34 }
35 set
36 {
37 DynamicByteProvider _DynamicByteProvider;
38 _DynamicByteProvider = new DynamicByteProvider(value);
39 editor.ByteProvider = _DynamicByteProvider;
40 }
41 }
42
43 public HexEditor()
44 {
45 InitializeComponent();
46 this.Value = default(List<byte>);
47 }
48 public HexEditor(List<byte> PixelBytes)
49 {
50 InitializeComponent();
51 this.Value = PixelBytes;
52 }
53 public HexEditor(object PixelBytes)
54 {
55 InitializeComponent();
56 this.Value = (List<byte>)(Convert.ChangeType(PixelBytes, typeof(List<byte>)));
57 }
58
59 private void HexEditor_Load(object sender, EventArgs e)
60 {
61 this.Text = "Hex Editor - Editing: " + Value.Count + " (" + string.Format("0x{0}", Value.Count.ToString("X8")) + ")" + " bytes";
62 this.AsBytes = Value.ToArray();
63 }
64
65 private void HexEditor_FormClosing(object sender, FormClosingEventArgs e)
66 {
67 this.Value.Clear();
68 this.Value = new List<byte>();
69 foreach (byte b in this.AsBytes)
70 {
71 this.Value.Add(b);
72 }
73 }
74
75 private void btnImport_Click(object sender, EventArgs e)
76 {
77 //throw new NotImplementedException("Import Has Not Been Implemented");
78 DialogResult result = PixelImporter.ShowDialog();
79 if (result != DialogResult.OK) return;
80
81 // read bytes from file
82 FileStream fs = new FileStream(PixelImporter.FileName, FileMode.Open);
83 List<byte> _pixels = new List<byte>();
84 BinaryReader r = new BinaryReader(fs);
85 r.BaseStream.Seek(0, SeekOrigin.Begin);
86 byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
87 r.Close();
88
89 foreach (byte b in bytes)
90 {
91 _pixels.Add(b);
92 }
93
94 MessageBox.Show("Successfully imported: " + _pixels.Count + " (" + string.Format("0x{0}", _pixels.Count.ToString("X8")) + ")" + " pixel bytes", "Pixel Data Import Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);
95 this.AsBytes = _pixels.ToArray();
96
97 }
98
99 private void btnExport_Click(object sender, EventArgs e)
100 {
101 //throw new NotImplementedException("Export Has Not Been Implemented");
102 DialogResult result = PixelExporter.ShowDialog();
103 if (result != DialogResult.OK) return;
104
105 try
106 {
107 FileStream fs = new FileStream(PixelExporter.FileName, FileMode.Create);
108 BinaryWriter w = new BinaryWriter(fs);
109 foreach (byte b in Value.ToArray())
110 {
111 w.Write(b);
112 }
113 w.Flush();
114 w.Close();
115 MessageBox.Show("Successfully exported: " + Value.Count + " (" + string.Format("0x{0}", Value.Count.ToString("X8")) + ")" + " pixel bytes", "Pixel Data Export Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);
116 }
117 catch (Exception ex)
118 {
119 MessageBox.Show("Failed to export pixel data!\n\nMessage: " + ex.Message, "Pixel Data Export Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
120 return;
121 }
122 }
123
124 }
125 }

  ViewVC Help
Powered by ViewVC 1.1.22