public ImageSource LoadImage(string uri, int width, int height, LoadImageOptions options) { ImageSource result = DoLoadImage(uri, width, height, options); OnImageLoad(uri, result); return(result); }
public ImageSource LoadImage(string uri, LoadImageOptions options) { ImageSource result = DoLoadImage(uri, null, null, options); OnImageLoad(uri, result); return(result); }
private ImageSource DoLoadImage(string uri, int?width, int?height, LoadImageOptions options) { // parse as URI and check for existence of file, return Uri for resource that exists // or null (note: resource is not necesarily allowed to be read) Uri imageUri = GetImageUri(uri); if (imageUri == null) { return(null); } // based on protocol/scheme, check to make sure source location is permitted if (!CheckImageLocationSecurity(imageUri)) { return(null); } if (uri.EndsWith(".xaml")) { return(imageUri.Scheme == "pack" ? LoadXamlResource(imageUri, width, height) : LoadXamlFile(imageUri, width, height)); } Logger.Debug("image being loaded from {URI}", Anonymizer.Anonymize(imageUri)); BitmapImage image = new BitmapImage(); image.BeginInit(); // Don't keep disk files open, because we may want to change them. But it is ok to lazy load from embedded resources that can't change. image.CacheOption = imageUri.Scheme == "pack" ? BitmapCacheOption.OnDemand : BitmapCacheOption.OnLoad; // Override BitMapCreateOptions but only for certain controls that require a reload of images image.CreateOptions = options.HasFlag(LoadImageOptions.ReloadIfChangedExternally) ? BitmapCreateOptions.IgnoreImageCache : BitmapCreateOptions.DelayCreation; image.UriSource = imageUri; // REVISIT: not clear if it is legal to set decoding in just one axis but we will find out if anyone ever uses this function with only one scaling factor if (width.HasValue) { image.DecodePixelWidth = Math.Max(1, width.Value); } if (height.HasValue) { image.DecodePixelHeight = Math.Max(1, height.Value); } image.EndInit(); return(image); }
/// <summary>Loads an icon.</summary> /// <param name="hinst"> /// A handle to the module of either a DLL or executable (.exe) that contains the image to be loaded. For more information, see /// GetModuleHandle. Note that as of 32-bit Windows, an instance handle (HINSTANCE), such as the application instance handle exposed /// by system function call of WinMain, and a module handle (HMODULE) are the same thing. /// <para>To load an OEM image, set this parameter to NULL.</para> /// <para>To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp—set this parameter to NULL.</para> /// </param> /// <param name="lpszName"> /// The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the /// image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a /// pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by /// ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the /// LoadImage function. For more information, see the Remarks section below. /// <para> /// If the hinst parameter is NULL and the fuLoad parameter omits the LR_LOADFROMFILE value, the lpszName specifies the OEM image to load. /// </para> /// <para> /// To pass these constants to the LoadImage function, use the MAKEINTRESOURCE macro. For example, to load the OCR_NORMAL cursor, /// pass MAKEINTRESOURCE(OCR_NORMAL) as the lpszName parameter, NULL as the hinst parameter, and LR_SHARED as one of the flags to the /// fuLoad parameter. /// </para> /// <para> /// If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the stand-alone /// resource (icon, cursor, or bitmap file). Therefore, set hinst to NULL. /// </para> /// </param> /// <param name="cxDesired"> /// The width, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and LR_DEFAULTSIZE is not used, /// the function uses the actual resource width. /// </param> /// <param name="cyDesired"> /// The height, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CYICON or SM_CYCURSOR system metric value to set the height. If this parameter is zero and LR_DEFAULTSIZE is not /// used, the function uses the actual resource height. /// </param> /// <param name="fuLoad">Loading options.</param> /// <returns> /// If the function succeeds, the return value is the handle of the newly loaded image. If the function fails, the return value is /// NULL.To get extended error information, call GetLastError. /// </returns> public static SafeHICON LoadImage_Icon(HINSTANCE hinst, SafeResourceId lpszName, int cxDesired, int cyDesired, LoadImageOptions fuLoad) => new SafeHICON(LoadImage(hinst, lpszName, LoadImageType.IMAGE_ICON, cxDesired, cyDesired, fuLoad), true);
/// <summary>Loads an enhanced metafile.</summary> /// <param name="hinst"> /// A handle to the module of either a DLL or executable (.exe) that contains the image to be loaded. For more information, see /// GetModuleHandle. Note that as of 32-bit Windows, an instance handle (HINSTANCE), such as the application instance handle exposed /// by system function call of WinMain, and a module handle (HMODULE) are the same thing. /// <para>To load an OEM image, set this parameter to NULL.</para> /// <para>To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp—set this parameter to NULL.</para> /// </param> /// <param name="lpszName"> /// The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the /// image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a /// pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by /// ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the /// LoadImage function. For more information, see the Remarks section below. /// <para> /// If the hinst parameter is NULL and the fuLoad parameter omits the LR_LOADFROMFILE value, the lpszName specifies the OEM image to load. /// </para> /// <para> /// To pass these constants to the LoadImage function, use the MAKEINTRESOURCE macro. For example, to load the OCR_NORMAL cursor, /// pass MAKEINTRESOURCE(OCR_NORMAL) as the lpszName parameter, NULL as the hinst parameter, and LR_SHARED as one of the flags to the /// fuLoad parameter. /// </para> /// <para> /// If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the stand-alone /// resource (icon, cursor, or bitmap file). Therefore, set hinst to NULL. /// </para> /// </param> /// <param name="cxDesired"> /// The width, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and LR_DEFAULTSIZE is not used, /// the function uses the actual resource width. /// </param> /// <param name="cyDesired"> /// The height, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CYICON or SM_CYCURSOR system metric value to set the height. If this parameter is zero and LR_DEFAULTSIZE is not /// used, the function uses the actual resource height. /// </param> /// <param name="fuLoad">Loading options.</param> /// <returns> /// If the function succeeds, the return value is the handle of the newly loaded image. If the function fails, the return value is /// NULL.To get extended error information, call GetLastError. /// </returns> public static SafeHENHMETAFILE LoadImage_EnhMetaFile(HINSTANCE hinst, SafeResourceId lpszName, int cxDesired, int cyDesired, LoadImageOptions fuLoad) => new SafeHENHMETAFILE(LoadImage(hinst, lpszName, LoadImageType.IMAGE_ENHMETAFILE, cxDesired, cyDesired, fuLoad), true);
/// <summary>Loads a cursor or animated cursor.</summary> /// <param name="hinst"> /// A handle to the module of either a DLL or executable (.exe) that contains the image to be loaded. For more information, see /// GetModuleHandle. Note that as of 32-bit Windows, an instance handle (HINSTANCE), such as the application instance handle exposed /// by system function call of WinMain, and a module handle (HMODULE) are the same thing. /// <para>To load an OEM image, set this parameter to NULL.</para> /// <para>To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp—set this parameter to NULL.</para> /// </param> /// <param name="lpszName"> /// The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the /// image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a /// pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by /// ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the /// LoadImage function. For more information, see the Remarks section below. /// <para> /// If the hinst parameter is NULL and the fuLoad parameter omits the LR_LOADFROMFILE value, the lpszName specifies the OEM image to load. /// </para> /// <para> /// To pass these constants to the LoadImage function, use the MAKEINTRESOURCE macro. For example, to load the OCR_NORMAL cursor, /// pass MAKEINTRESOURCE(OCR_NORMAL) as the lpszName parameter, NULL as the hinst parameter, and LR_SHARED as one of the flags to the /// fuLoad parameter. /// </para> /// <para> /// If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the stand-alone /// resource (icon, cursor, or bitmap file). Therefore, set hinst to NULL. /// </para> /// </param> /// <param name="cxDesired"> /// The width, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and LR_DEFAULTSIZE is not used, /// the function uses the actual resource width. /// </param> /// <param name="cyDesired"> /// The height, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CYICON or SM_CYCURSOR system metric value to set the height. If this parameter is zero and LR_DEFAULTSIZE is not /// used, the function uses the actual resource height. /// </param> /// <param name="fuLoad">Loading options.</param> /// <returns> /// If the function succeeds, the return value is the handle of the newly loaded image. If the function fails, the return value is /// NULL.To get extended error information, call GetLastError. /// </returns> public static SafeHCURSOR LoadImage_Cursor(HINSTANCE hinst, SafeResourceId lpszName, int cxDesired, int cyDesired, LoadImageOptions fuLoad) => new SafeHCURSOR(LoadImage(hinst, lpszName, LoadImageType.IMAGE_CURSOR, cxDesired, cyDesired, fuLoad), true);
/// <summary>Loads a bitmap.</summary> /// <param name="hinst"> /// A handle to the module of either a DLL or executable (.exe) that contains the image to be loaded. For more information, see /// GetModuleHandle. Note that as of 32-bit Windows, an instance handle (HINSTANCE), such as the application instance handle exposed /// by system function call of WinMain, and a module handle (HMODULE) are the same thing. /// <para>To load an OEM image, set this parameter to NULL.</para> /// <para>To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp—set this parameter to NULL.</para> /// </param> /// <param name="lpszName"> /// The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the /// image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a /// pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by /// ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the /// LoadImage function. For more information, see the Remarks section below. /// <para> /// If the hinst parameter is NULL and the fuLoad parameter omits the LR_LOADFROMFILE value, the lpszName specifies the OEM image to load. /// </para> /// <para> /// To pass these constants to the LoadImage function, use the MAKEINTRESOURCE macro. For example, to load the OCR_NORMAL cursor, /// pass MAKEINTRESOURCE(OCR_NORMAL) as the lpszName parameter, NULL as the hinst parameter, and LR_SHARED as one of the flags to the /// fuLoad parameter. /// </para> /// <para> /// If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the stand-alone /// resource (icon, cursor, or bitmap file). Therefore, set hinst to NULL. /// </para> /// </param> /// <param name="cxDesired"> /// The width, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and LR_DEFAULTSIZE is not used, /// the function uses the actual resource width. /// </param> /// <param name="cyDesired"> /// The height, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function /// uses the SM_CYICON or SM_CYCURSOR system metric value to set the height. If this parameter is zero and LR_DEFAULTSIZE is not /// used, the function uses the actual resource height. /// </param> /// <param name="fuLoad">Loading options.</param> /// <returns> /// If the function succeeds, the return value is the handle of the newly loaded image. If the function fails, the return value is /// NULL.To get extended error information, call GetLastError. /// </returns> public static SafeHBITMAP LoadImage_Bitmap(HINSTANCE hinst, SafeResourceId lpszName, int cxDesired, int cyDesired, LoadImageOptions fuLoad) => new SafeHBITMAP(LoadImage(hinst, lpszName, LoadImageType.IMAGE_BITMAP, cxDesired, cyDesired, fuLoad), true);
public static extern IntPtr LoadImage(HINSTANCE hinst, SafeResourceId lpszName, LoadImageType uType, int cxDesired, int cyDesired, LoadImageOptions fuLoad);
public static extern IntPtr LoadImage(IntPtr hinst, [MarshalAs(UnmanagedType.SysInt)] int uID, LoadImageType uType, int cxDesired, int cyDesired, LoadImageOptions fuLoad);
public static extern IntPtr LoadImage(IntPtr hinst, string lpszName, LoadImageType uType, int cxDesired, int cyDesired, LoadImageOptions fuLoad);
public static extern IntPtr LoadImage(SafeLibraryHandle hinst, SafeResourceId lpszName, LoadImageType uType, int cxDesired, int cyDesired, LoadImageOptions fuLoad);