Skip to content

Commit

Permalink
Added functions to AlphaBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
harborsiem committed Mar 4, 2022
1 parent 7f20c8f commit 134e9a1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

### Ribbon V2.11.1, RibbonTools V1.3.7+

#### Changed (RibbonTools)

- Added functions (and small changes) to AlphaBitmap, NativeMethods

### Ribbon V2.11.1, RibbonTools V1.3.7

#### Changed (RibbonTools)
Expand Down
12 changes: 11 additions & 1 deletion RibbonTools/CommandsFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CommandsFrame()
components = new Container();

_listViewColumnSorter = new ListViewColumnSorter();
_listViewTimer = new Timer(); //@ added, because we don't want unEnabled the right site after each selection change
_listViewTimer = new Timer(); //@ added, because we don't want disabled the right site after each selection change
_listViewTimer.Interval = 100;

bool runtime = (LicenseManager.UsageMode == LicenseUsageMode.Runtime);
Expand Down Expand Up @@ -454,6 +454,16 @@ private void EditCaptionChange(object sender, EventArgs e)
ListViewCommands.SelectedItems[0].SubItems[1].Text = EditCaption.Text;
Modified();
}
//if (_command != null)
//{
// string textboxContent = TextboxToContent(EditCaption.Text);
// if (_command.LabelTitle.Content != textboxContent)
// {
// _command.LabelTitle.Content = textboxContent;
// ListViewCommands.SelectedItems[0].SubItems[1].Text = EditCaption.Text;
// Modified();
// }
//}
}

private void EditCaptionIdChange(object sender, EventArgs e)
Expand Down
100 changes: 82 additions & 18 deletions RibbonTools/Misc/AlphaBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private static Bitmap TryConvertToAlphaBitmap(Bitmap bitmap)
BitmapData alphaData = alpha.LockBits(new Rectangle(new Point(), alpha.Size), ImageLockMode.ReadWrite, alpha.PixelFormat);
Marshal.Copy(bmpScan, 0, alphaData.Scan0, length);
alpha.UnlockBits(alphaData);
bitmap.Dispose();
return alpha;
}
}
Expand Down Expand Up @@ -101,8 +102,7 @@ private static byte[] IsBmpFile(string fileName)
byte[] bytes = File.ReadAllBytes(fileName);
if (bytes.Length > 54)
{
string headerMark = Encoding.ASCII.GetString(bytes, 0, 2);
if (headerMark.Equals("BM"))
if (bytes[0] == 0x42 && bytes[1] == 0x4d) //"BM"
{
return bytes;
}
Expand All @@ -116,6 +116,7 @@ private static byte[] IsBmpFile(string fileName)
/// <param name="fileName"></param>
/// <param name="highContrast"></param>
/// <returns>The Bitmap with fully transparency if available</returns>
[Obsolete("Please use function TryAlphaBitmapFromFile")]
public static Bitmap BitmapFromFile(string fileName, bool highContrast = false)
{
Bitmap bitmap = null;
Expand Down Expand Up @@ -160,11 +161,11 @@ public static Bitmap BitmapFromFile(string fileName, bool highContrast = false)
}

/// <summary>
/// Get the managed ARGB Bitmap
/// Get the managed ARGB Bitmap if possible (32 bit per pixel)
/// </summary>
/// <param name="hBitmap">Handle to a Bitmap</param>
/// <returns>The Bitmap with fully transparency if available</returns>
public static Bitmap GetManagedARGBBitmap(IntPtr hBitmap)
public static Bitmap FromHbitmap(IntPtr hBitmap)
{
// Create the BITMAP structure and get info from our nativeHBitmap
NativeMethods.BITMAP bitmapStruct = new NativeMethods.BITMAP();
Expand All @@ -182,7 +183,7 @@ public static Bitmap GetManagedARGBBitmap(IntPtr hBitmap)
else
{
managedBitmap = Bitmap.FromHbitmap(hBitmap);
managedBitmap.MakeTransparent();
//managedBitmap.MakeTransparent();
}
return managedBitmap;
}
Expand All @@ -198,25 +199,67 @@ public static Bitmap ImageFromFile(string path)
throw new ArgumentNullException(nameof(path));
if (!File.Exists(path))
throw new ArgumentException("File does not exist", nameof(path));
char[] buffer = new char[2];
StreamReader sr = File.OpenText(path);
int length = sr.ReadBlock(buffer, 0, 2);
long baseLength = sr.BaseStream.Length;
sr.Close();
if (baseLength > 54 && length == 2 && buffer[0] == 'B' && buffer[1] == 'M')
byte[] bytes = new byte[2];
FileStream stream = File.OpenRead(path);
if (stream.Length > 54) //minimum length of a bitmap file
stream.Read(bytes, 0, 2);
stream.Close();
if (bytes[0] == 0x42 && bytes[1] == 0x4d) //"BM"
{
IntPtr handle = IntPtr.Zero;
handle = NativeMethods.LoadImage(IntPtr.Zero, path, (uint)NativeMethods.ImageType.IMAGE_BITMAP, 0, 0,
(uint)(NativeMethods.ImageLoad.LR_LOADFROMFILE | NativeMethods.ImageLoad.LR_CREATEDIBSECTION));
return GetManagedARGBBitmap(handle);
if (handle != IntPtr.Zero)
return FromHbitmap(handle);
return null;
}
try
{
Bitmap bitmap = new Bitmap(path);
if ((int)bitmap.HorizontalResolution != 96)
{
bitmap.SetResolution(96.0f, 96.0f); //only png bitmaps can have other resolution
}
return bitmap;
}
catch (Exception)
{
return null;
}
}

Bitmap bitmap = new Bitmap(path);
if ((int)bitmap.HorizontalResolution != 96)
/// <summary>
/// Load a Bitmap (with transparency) from a native resource dll via Windows API
/// The resourceHandle should be Ribbon.MarkupHandle, id is an Image id from RibbonMarkup.h
/// </summary>
/// <param name="resourceHandle">LoadLibrary handle (Ribbon.MarkupHandle)</param>
/// <param name="id">id of the image (from headerfile RibbonMarkup.h)</param>
/// <returns>The Bitmap with fully transparency if available</returns>
public static Bitmap ImageFromResource(IntPtr resourceHandle, uint id)
{
if (resourceHandle == IntPtr.Zero)
throw new ArgumentNullException(nameof(resourceHandle));
IntPtr bmpHandle = NativeMethods.LoadImage(resourceHandle, (IntPtr)id, (uint)NativeMethods.ImageType.IMAGE_BITMAP, 0, 0, (uint)NativeMethods.ImageLoad.LR_CREATEDIBSECTION);
if (bmpHandle != IntPtr.Zero)
{
bitmap.SetResolution(96.0f, 96.0f); //only png bitmaps can have other resolution
return FromHbitmap(bmpHandle);
}
return bitmap;
//Lookup for the Bitmap resource in the resource folder IMAGE
//Maybe it is a Bitmap V5 or a PNG Bitmap
IntPtr hResource = NativeMethods.FindResource(resourceHandle, (IntPtr)id, "IMAGE");
if (hResource == IntPtr.Zero)
return null;
uint imageSize = NativeMethods.SizeofResource(resourceHandle, hResource);
if (imageSize == 0)
return null;
IntPtr res = NativeMethods.LoadResource(resourceHandle, hResource);
IntPtr pResourceData = NativeMethods.LockResource(res);
if (pResourceData == IntPtr.Zero)
return null;
byte[] imageData = new byte[imageSize];
Marshal.Copy(pResourceData, imageData, 0, (int)imageSize);
MemoryStream stream = new MemoryStream(imageData);
return new Bitmap(stream);
}

/// <summary>
Expand Down Expand Up @@ -251,7 +294,6 @@ public static Bitmap SetTransparentRGB(Bitmap bitmap, int transparentRGB)

class NativeMethods
{

public enum BitmapCompressionMode : uint
{
BI_RGB = 0,
Expand Down Expand Up @@ -331,7 +373,11 @@ public struct BITMAP
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType,
public static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint type,
int cxDesired, int cyDesired, uint fuLoad);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadImage(IntPtr hinst, IntPtr lpszName, uint type,
int cxDesired, int cyDesired, uint fuLoad);

public enum ImageType
Expand Down Expand Up @@ -372,6 +418,24 @@ public enum ImageLoad
//This function finds the first image in the cache with the requested resource name, regardless of the size requested.
LR_VGACOLOR = 0x00000080
}

[DllImport("kernel32.dll")]
public static extern IntPtr FindResource(IntPtr hModule, string lpName, IntPtr lpType);

[DllImport("kernel32.dll")]
public static extern IntPtr FindResource(IntPtr hModule, IntPtr lpName, IntPtr lpType);

[DllImport("kernel32.dll")]
public static extern IntPtr FindResource(IntPtr hModule, IntPtr lpName, string lpType);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);

[DllImport("kernel32.dll")]
public static extern IntPtr LockResource(IntPtr hResData);
}
}
}
3 changes: 3 additions & 0 deletions RibbonTools/Misc/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ public enum DeviceCap
LOGPIXELSY = 90,
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

public double DpiScaling()
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
Expand Down

0 comments on commit 134e9a1

Please sign in to comment.