Skip to content

Commit

Permalink
Began resource refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Aug 11, 2016
1 parent 1afcb1e commit 96e4c7f
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 262 deletions.
7 changes: 5 additions & 2 deletions Src/Debug Application/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ static void Main(string[] args)
}

ExecutableImage image = ExecutableImage.FromFile(file_name);
DelayImportDirectory directory = DelayImportDirectory.Get(image);
DelayImportAddressTables tables = DelayImportAddressTables.GetAddressTable(directory);

BitmapResource.Register();

ResourceCollection resources = ResourceCollection.Get(image);
ResourceType resource_type = resources.First(type => type.Id == ResourceType.RT_BITMAP);
Resource resource = resource_type.FirstOrDefault(res => res.Id == 135);

}

Expand Down
124 changes: 28 additions & 96 deletions Src/Workshell.PE.Resources/Graphics/BitmapResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,23 @@ namespace Workshell.PE.Resources
public enum BitmapSaveFormat
{
Raw,
Resource,
Bitmap
}

public class BitmapResource
public sealed class BitmapResource : Resource
{

private Resource resource;
private uint language_id;
private byte[] dib;

internal BitmapResource(Resource sourceResource, uint languageId, byte[] dibData)
public BitmapResource(ResourceType owningType, ResourceDirectoryEntry directoryEntry) : base(owningType, directoryEntry)
{
resource = sourceResource;
language_id = languageId;
dib = dibData;
}

#region Static Methods

public static BitmapResource Load(Resource resource)
{
return Load(resource, Resource.DEFAULT_LANGUAGE);
}

public static BitmapResource Load(Resource resource, uint language)
public static bool Register()
{
if (!resource.Languages.Contains(language))
return null;

if (resource.Type.Id != ResourceType.RT_BITMAP)
return null;

byte[] data = resource.GetBytes(language);
BitmapResource result = new BitmapResource(resource, language, data);
ResourceId resource_type = new ResourceId(ResourceType.RT_BITMAP);

return result;
return ResourceType.Register(resource_type, typeof(BitmapResource));
}

#endregion
Expand All @@ -89,11 +69,17 @@ public static BitmapResource Load(Resource resource, uint language)

public Bitmap ToBitmap()
{
MemoryStream mem = resource.Type.Resources.Image.MemoryStreamProvider.GetStream();
return ToBitmap(DEFAULT_LANGUAGE);
}

public Bitmap ToBitmap(uint languageId)
{
MemoryStream mem = new MemoryStream();

using (mem)
{
MemoryStream dib_mem = resource.Type.Resources.Image.MemoryStreamProvider.GetStream(dib);
byte[] dib = GetBytes(languageId);
MemoryStream dib_mem = new MemoryStream(dib);

using (dib_mem)
{
Expand All @@ -112,90 +98,36 @@ public Bitmap ToBitmap()

mem.Seek(0, SeekOrigin.Begin);

Bitmap bitmap = (Bitmap)Image.FromStream(mem);
using (Bitmap bitmap = (Bitmap)Image.FromStream(mem))
{
Bitmap result = new Bitmap(bitmap); // Create a copy of the image so we can release the stream

return bitmap;
return result;
}
}
}

public void Save(string fileName)
{
Save(fileName, BitmapSaveFormat.Bitmap);
}

public void Save(Stream stream)
{
Save(stream, BitmapSaveFormat.Bitmap);
}

public void Save(string fileName, BitmapSaveFormat format)
public void Save(string fileName, uint languageId, BitmapSaveFormat format)
{
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
Save(file, format);
Save(file, languageId, format);
file.Flush();
}
}

public void Save(Stream stream, BitmapSaveFormat format)
public void Save(Stream stream, uint languageId, BitmapSaveFormat format)
{
switch (format)
if (format == BitmapSaveFormat.Raw)
{
case BitmapSaveFormat.Raw:
SaveRaw(stream);
break;
case BitmapSaveFormat.Resource:
SaveResource(stream);
break;
case BitmapSaveFormat.Bitmap:
SaveBitmap(stream);
break;
Save(stream, languageId);
}
}

private void SaveRaw(Stream stream)
{
byte[] data = resource.GetBytes(language_id);

stream.Write(data, 0, data.Length);
}

private void SaveResource(Stream stream)
{
throw new NotImplementedException();
}

private void SaveBitmap(Stream stream)
{
using (Bitmap bitmap = ToBitmap())
bitmap.Save(stream, ImageFormat.Bmp);
}

#endregion

#region Properties

public Resource Resource
{
get
{
return resource;
}
}

public uint Language
{
get
else
{
return language_id;
}
}

public byte[] DIB
{
get
{
return dib;
using (Bitmap bitmap = ToBitmap(languageId))
{
bitmap.Save(stream, ImageFormat.Bmp);
}
}
}

Expand Down
Loading

0 comments on commit 96e4c7f

Please sign in to comment.