C# 将渲染器转换为位图

C# 将渲染器转换为位图,c#,gdi+,C#,Gdi+,我需要通过VisualStyleRenderer绘制一个不同的进度条。如果我使用OnPaint方法的Graphics,一切正常。但由于我想将其保存在硬盘中,我需要在位图对象中渲染progressbar,然后保存它 下面是示例代码 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawImage(RenderProgressbarImage(), new Point(5

我需要通过
VisualStyleRenderer
绘制一个不同的进度条。如果我使用
OnPaint
方法的
Graphics
,一切正常。但由于我想将其保存在硬盘中,我需要在
位图
对象中渲染progressbar,然后保存它

下面是示例代码

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;
}
但如果我在位图中绘制它,它有黑色的角而不是透明的。但是,如果它使用
OnPaint
图形
,一切都会很好


使用
位图
,您将使用GDI+创建一个矩形对象

可能会帮助您创建所需的圆形位图图像

编辑-修改了
RenderProgressbarImage
以将
图形
对象作为输入

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));
}
protected override void OnPaint(PaintEventArgs e)
{
基础漆(e);
位图bmp=新位图(100、13、e.Graphics);
bmp.Save();
//下面的代码运行良好
progressRenderer.SetParameters(“PROGRESS”,11,2);
牵引地面(例如图形,新矩形(125,5100,13));
}

注意:在
OnPaint
事件中保存
Bitmap
会对渲染造成一定的性能影响。也许只需更新类中的
位图
变量,并定期从不同的
线程
/some
计时器
/etc保存
位图
。;这完全取决于你的需要。

我知道它很旧,但我也面临着同样的问题,经过大量研究,我找到了一个解决方案,我希望它能帮助别人

// 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:
URL2:

URL3:

创建圆角图像是另一回事。那不是我想要的。我需要使用VisualStyleRenderer绘制更多内容。让我看看是否可以为您提供一个工作示例。更重要的是,我将向您介绍在这种情况下用于处理拐角的
图形
例程
e.Graphics
已经在
OnPaint
事件中工作了对不起,我不明白你想说什么。图形
对象是非圆角的原因;使用该问题中的圆角
Graphics
例程是一个选项,但从
OnPaint
传递
Graphics
是最简单的解决方案是的,但我不需要在控件上绘制,我需要它保存为png。只有使用“OnPaint”绘制它,然后调用该控件的“DrawToBitmap”,这才有可能,但这不是一个好主意,非常糟糕。