简体   繁体   中英

Convert an ImageMoniker to a Bitmap for a Visual Studio extension

I want to display a status animation in the Visual Studio status bar. For the animation I need a bitmap. To prevent having duplicate files I want to get that bitmap from a given ImageMoniker that I have linked in my VSCT file.

My current code looks like this:

private void ShowIconInStatusBar(object sender, RatingChangedEventArgs eventArgs)
{
    IntPtr hdc = new Bitmap(@"Resources\red.bmp").GetHbitmap();
    object icon = hdc;

    this.statusBar.Animation(1, ref icon);
}

Ideally I would use the ImageMoniker though. I imagine something like this:

Bitmap b = Bitmap.FromImageMoniker(this.moniker);

But I can't find any way to cast an ImageMoniker to an Image or a Bitmap. How would I do that? Is it even possible to do?

Doing this directly doesn't seem to work, but I found a different solution without the need for duplicate files.

var bitmap = new Bitmap(path).Clone(
    new Rectangle(
        new Point(xCoordinate, 0),
        new Size(16, 16)),
    System.Drawing.Imaging.PixelFormat.DontCare);
IntPtr hdc = bitmap.GetHbitmap();

By using Clone I can achieve the same functionality that I got from the ImageMoniker - using only one file for all images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM