PropertyGrid - 获取位图的文件路径

时间:2013-01-03 20:10:10

标签: c# .net winforms bitmap propertygrid

我将PropertyGrid的{​​{1}}设置为具有SelectedObject属性的对象:

Bitmap

这会自动显示public class Obj { private Bitmap myimage; [Category("Main Category")] [Description("Your favorite image")] public Bitmap MyImage { get { return myimage; } set { myimage = value; } } } 中的属性,并带有一个用户可以单击以选择图像的小按钮。如何获取此图像的文件路径?

(另外,有什么方法可以覆盖弹出的对话框并放入我自己的对话框吗?我知道这是一个单独的问题,所以我可能会单独发布。)

1 个答案:

答案 0 :(得分:3)

  

如何获取此图片的文件路径?   另外,有什么方法可以覆盖弹出的对话框并将我自己的对话框放入?

实现这两种方式的方法是创建一个新的UITypeEditor

public class BitmapLocationEditor : UITypeEditor
{

}

我覆盖了GetEditStyleEditValueGetPaintvalueSupportedPaintValue方法。

// Displays an ellipsis (...) button to start a modal dialog box
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
    return UITypeEditorEditStyle.Modal;
}

// Edits the value of the specified object using the editor style indicated by the GetEditStyle method.
public override object EditValue(ITypeDescriptorContext context,
                                 IServiceProvider provider,
                                 object value)
{
    // Show the dialog we use to open the file.
    // You could use a custom one at this point to provide the file path and the image.
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.Filter = "Image files (*.bmp | *.bmp;";

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            Bitmap bitmap = new Bitmap(openFileDialog.FileName);
            bitmap.SetPath(openFileDialog.FileName);

            return bitmap;
        }
    }

    return value;
}

// Indicates whether the specified context supports painting
// a representation of an object's value within the specified context.
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
    return true;
}

// Paints a representation of the value of an object using the specified PaintValueEventArgs.
public override void PaintValue(PaintValueEventArgs e)
{
    if (e.Value != null)
    {
        // Get image
        Bitmap bmp = (Bitmap)e.Value;

        // This rectangle indicates the area of the Properties window to draw a representation of the value within.
        Rectangle destRect = e.Bounds;

        // Optional to set the default transparent color transparent for this image.
        bmp.MakeTransparent();

        // Draw image
        e.Graphics.DrawImage(bmp, destRect);
    }
}

除了添加属性以利用新的Obj类之外,您的BitmapLocationEditor类应该保持不变:

public class Obj
{
    private Bitmap myImage;

    [Category("Main Category")]
    [Description("Your favorite image")]
    [Editor(typeof(BitmapLocationEditor), typeof(UITypeEditor))]
    public Bitmap MyImage
    {
        get { return myImage; }
        set
        {
            myImage = value;
        }
    }
}

SetPath方法中使用的EditValue方法只是设置Bitmap.Tag属性的扩展方法。稍后您可以使用GetPath方法检索图像文件的实际路径。

public static class BitmapExtension
{
    public static void SetPath(this Bitmap bitmap, string path)
    {
        bitmap.Tag = path;
    }

    public static string GetPath(this Bitmap bitmap)
    {
        return bitmap.Tag as string;
    }
}

Article about PropertyGrid