5

我需要通过VisualStyleRenderer. Graphics如果我使用方法,一切都很好OnPaint。但是由于我想将它保存在硬盘中,我需要在Bitmap对象中渲染进度条然后保存它。

这是示例代码

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawImage(RenderProgressbarImage(), new Point(5, 5));

    //following code works good
    progressRenderer.SetParameters("PROGRESS", 11, 2);
    progressRenderer.DrawBackground(e.Graphics, new Rectangle(125, 5, 100, 13));
}
VisualStyleRenderer progressRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Bar.Normal);
Bitmap RenderProgressbarImage()
{
    Bitmap bmp = new Bitmap(100, 13);
    using (Graphics g = Graphics.FromImage((Image)bmp))
    {
        progressRenderer.SetParameters("PROGRESS", 11, 2);
        progressRenderer.DrawBackground(g, new Rectangle(0, 0, bmp.Width, bmp.Height));                
    }
    return bmp;
}

但是如果我在位图中绘制它,它有黑角而不是透明的。但是,如果它使用GraphicsOnPaint一切都很好。

截屏

4

2 回答 2

1

使用Bitmap,您将按照您的方式使用 GDI+ 制作一个矩形对象。

创建带圆角的图像 可能会帮助您根据需要创建圆角位图图像。

编辑- 修改RenderProgressbarImage为将Graphics对象作为输入

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawImage(RenderProgressbarImage(e.Graphics), new Point(5, 5));

    //Test to Check for Output
    RenderProgressbarImage(e.Graphics).Save(@"C:\Bitmap.bmp");;

    //following code works good
    progressRenderer.SetParameters("PROGRESS", 11, 2);
    progressRenderer.DrawBackground(e.Graphics, new Rectangle(125, 5, 100, 13));
}
Bitmap RenderProgressbarImage(Graphics g)
{
    Bitmap bmp = new Bitmap(100, 13, g);
    progressRenderer.SetParameters("PROGRESS", 11, 2);
    progressRenderer.DrawBackground(g, new Rectangle(0, 0, bmp.Width, bmp.Height));

    return bmp;
}

Edit2:根据下面的 OP 评论进行了修改以简化解决方案

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Bitmap bmp = new Bitmap(100, 13, e.Graphics);
    bmp.Save(<SomefilePath.png>);

    //following code works good
    progressRenderer.SetParameters("PROGRESS", 11, 2);
    progressRenderer.DrawBackground(e.Graphics, new Rectangle(125, 5, 100, 13));
}

关于这一点的注意事项:BitmapOnPaint事件中保存 将对渲染产生明确的性能影响。也许只是更新Bitmap你班级中的一个变量,并Bitmap定期从不同的Thread/一些Timer/等中保存它;这一切都取决于您的需求。

于 2013-04-16T07:22:41.963 回答
1

我知道它很旧,但我遇到了同样的问题,经过大量研究,我找到了解决方案,我希望它对某人有所帮助。

// Created by: Motaz Alnuweiri

// Reference:
// URL1: https://www.autoitscript.com/forum/topic/181956-drawthemebackground-bitmap-alpha/
// URL2: https://gist.github.com/wavescholar/11297223#file-gdi-bitmap-conversion-L71
// URL3: https://www.experts-exchange.com/questions/20872978/BITMAPINFOHEADER-from-NET-Bitmap.html


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

public class Helper
{
    #region Win32 Native APIs
    internal class NativeMethods
    {
        // CreateDIBSection funcation iUsage value
        internal const int DIB_RGB_COLORS = 0x00;
        internal const int DIB_PAL_COLORS = 0x01;
        internal const int DIB_PAL_INDICES = 0x02;

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern bool DeleteObject(IntPtr hObject);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern int InvalidateRect(IntPtr hwnd, IntPtr rect, int bErase);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern int DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO bmi, uint iUsage,
            out IntPtr bits, IntPtr hSection, uint dwOffset);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        internal static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

        [StructLayout(LayoutKind.Sequential)]
        internal struct BITMAPINFO
        {
            public Int32 biSize;
            public Int32 biWidth;
            public Int32 biHeight;
            public Int16 biPlanes;
            public Int16 biBitCount;
            public Int32 biCompression;
            public Int32 biSizeImage;
            public Int32 biXPelsPerMeter;
            public Int32 biYPelsPerMeter;
            public Int32 biClrUsed;
            public Int32 biClrImportant;
        }
    }
    #endregion

    public static Image VisualStyleRendererToImage(VisualStyleElement element, Rectangle bounds)
    {
        if (ToolStripManager.VisualStylesEnabled && VisualStyleRenderer.IsElementDefined(element))
        {
            VisualStyleRenderer renderer = new VisualStyleRenderer(element);

            using (Bitmap bit = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb))
            {
                NativeMethods.BITMAPINFO bmi = new NativeMethods.BITMAPINFO();

                bmi.biWidth = bit.Width;
                bmi.biHeight = bit.Height;
                bmi.biPlanes = 1;
                bmi.biBitCount = 32;
                bmi.biXPelsPerMeter = (int)bit.HorizontalResolution;
                bmi.biYPelsPerMeter = (int)bit.VerticalResolution;
                bmi.biSize = Marshal.SizeOf(typeof(NativeMethods.BITMAPINFO));

                IntPtr bits;
                IntPtr bmp = NativeMethods.CreateDIBSection(IntPtr.Zero, ref bmi,
                    NativeMethods.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0);

                IntPtr dc = NativeMethods.GetDC(IntPtr.Zero);
                IntPtr hdc = NativeMethods.CreateCompatibleDC(dc);
                NativeMethods.SelectObject(hdc, bmp);

                using (Graphics g = Graphics.FromHdc(hdc))
                {
                    renderer.DrawBackground(g, bounds);
                }

                Bitmap image = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb);

                using (Bitmap tempImage = new Bitmap(bounds.Width, bounds.Height, bounds.Width * 4,
                    PixelFormat.Format32bppPArgb, bits))
                {
                    BitmapData tempBitmapData = tempImage.LockBits(bounds, ImageLockMode.ReadOnly,
                        PixelFormat.Format32bppPArgb);
                    BitmapData bitmapData = image.LockBits(bounds, ImageLockMode.WriteOnly,
                        PixelFormat.Format32bppPArgb);

                    NativeMethods.CopyMemory(bitmapData.Scan0, tempBitmapData.Scan0,
                        (uint)tempBitmapData.Stride * (uint)tempBitmapData.Height);

                    tempImage.UnlockBits(tempBitmapData);
                    image.UnlockBits(bitmapData);
                }

                NativeMethods.DeleteObject(bmp);
                NativeMethods.DeleteDC(hdc);
                NativeMethods.ReleaseDC(IntPtr.Zero, dc);

                return image;
            }
        }
        else
        {
            return new Bitmap(bounds.Width, bounds.Height);
        }
    }
}

参考:
URL1:https
://www.autoitscript.com/forum/topic/181956-drawthemebackground-bitmap-alpha/ URL2:https ://gist.github.com/wavescholar/11297223#file-gdi-bitmap-conversion- L71
URL3:https ://www.experts-exchange.com/questions/20872978/BITMAPINFOHEADER-from-NET-Bitmap.html

于 2018-04-28T09:20:59.203 回答