public static Bitmap Load(ResourceLoader loader, string resourceName) { using (HGlobal buffer = HGlobal.WithStringUni(resourceName)) { return(new Bitmap(NativeMethods.LoadBitmap(loader.ModuleHandle, buffer.Handle))); } }
public void IntConstructor([Values(sizeof(int), sizeof(int) + 1)] int size) { using (var h = HGlobal.Create <int>(size)) { Assert.AreEqual(size, h.Length); Assert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, h.Value); } }
public void DefaultConstructor() { using (var h = HGlobal.Create <int>()) { Assert.AreEqual(sizeof(int), h.Length); Assert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, h.Value); } }
public void IntConstructor() { using (var h = HGlobal.Create(sizeof(int))) { Assert.AreEqual(sizeof(int), h.Length); Assert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, Marshal.ReadInt32(h.Data)); } }
/// <summary> /// 读完所有配置 /// </summary> public override void afterReadConfigAllOne(int type) { base.afterReadConfigAllOne(type); switch (type) { case ConfigType.Global: { HGlobal.afterReadConfigAll(); } break; } }
/// <summary> /// 设置值到Config上 /// </summary> public override void setToConfigOne(int type) { base.setToConfigOne(type); switch (type) { case ConfigType.Global: { HGlobal.readFromData((HGlobalReadData)global); HGlobal.afterReadConfig(); } break; } }
/// <summary> /// Loads an image (of any format, including PNG or JPEG) that is embedded in a DLL. /// </summary> /// <param name="loader"> /// A <see cref="ResourceLoader"/> instance used to load the resource. /// </param> /// <param name="resourceTypeName"> /// The name of the resource type (for example, <c>IMAGE</c>). /// </param> /// <param name="resourceName"> /// The name (as used in the RC file) of the resource to load. /// </param> /// <returns> /// A <see cref="Bitmap"/> instance that must be properly disposed to avoid leaking memory. /// </returns> public static Bitmap LoadImageFromResource(ResourceLoader loader, string resourceTypeName, string resourceName) { IntPtr hResInfo; using (HGlobal namePtr = HGlobal.WithStringUni(resourceName)) { hResInfo = NativeMethods.FindResourceW(loader.ModuleHandle, resourceTypeName, namePtr.Handle); } IntPtr hResData = NativeMethods.LoadResource(loader.ModuleHandle, hResInfo); IntPtr data = NativeMethods.LockResource(hResData); int size = NativeMethods.SizeofResource(loader.ModuleHandle, hResData); if (RuntimeInformation.FrameworkDescription.Contains(".NET Native")) { throw new PlatformNotSupportedException(); } else { byte[] buffer = new byte[size]; Marshal.Copy(data, buffer, 0, size); IntPtr hMem = Marshal.AllocHGlobal(size); Marshal.Copy(buffer, 0, hMem, size); NativeMethods.IStream stream = null; try { int hr = NativeMethods.CreateStreamOnHGlobal(hMem, true, out stream); if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } NativeMethods.IWICImagingFactory factory = (NativeMethods.IWICImagingFactory) new NativeMethods.WICImagingFactory(); NativeMethods.IWICBitmapDecoder decoder = factory.CreateDecoderFromStream(stream, options: NativeMethods.WICDecodeOptions.WICDecodeMetadataCacheOnDemand); decoder.GetFrame(0, out NativeMethods.IWICBitmapFrameDecode frame); return(LoadImageFromBitmapSource((NativeMethods.IWICBitmapSource)frame)); } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } } }
public void IntConstructorSmallerSize() { Assert.Throws <ArgumentException>(() => HGlobal.Create <int>(sizeof(int) - 1)); }