试着重新跟上速度。如果这是个过于简单的问题,我很抱歉。
我已经从MSDN创建了PictureViewer示例,并试图添加一些附加功能。具体来说,我试图从图像文件中读取和显示特定的MetaData。
在试图理解这个示例时,我似乎需要链接一个表单控件来调用示例代码。但我不明白这个电话应该是什么样子。放置
ExtractMetadata(e); in an event handler giving me a error集合1:无法从“System.EventArgs”转换为“System.Windows.Forms.PaintEventArgs”
我是不是漏掉了“this”指针什么的?
/
我想我需要更多的信息。如果您查看注释顶部的链接,ExtractMetadata是一个从图像文件获取EXIF数据(可交换图像文件)的调用,但它需要PaintEventArgs e,这是PaintEventArgs事件处理程序的一个参数,因此它需要System.Drawing.Imaging命名空间。
下面是示例提供的代码,我正在尝试执行:
private void ExtractMetaData(PaintEventArgs e)
{
try
{
// Create an Image object.
Image theImage = new Bitmap("C:\\Users\\DadPC\\Desktop\\testrunfiles\\fakePhoto.jpg");
// Get the PropertyItems property from image.
PropertyItem[] propItems = theImage.PropertyItems;
// Set up the display.
Font font1 = new Font("Arial", 10);
SolidBrush blackBrush = new SolidBrush(Color.Black);
int X = 0;
int Y = 0;
// For each PropertyItem in the array, display the id,
// type, and length.
int count = 0;
foreach (PropertyItem propItem in propItems)
{
e.Graphics.DrawString("Property Item " +
count.ToString(), font1, blackBrush, X, Y);
Y += font1.Height;
e.Graphics.DrawString(" ID: 0x" +
propItem.Id.ToString("x"), font1, blackBrush, X, Y);
Y += font1.Height;
e.Graphics.DrawString(" type: " +
propItem.Type.ToString(), font1, blackBrush, X, Y);
Y += font1.Height;
e.Graphics.DrawString(" length: " +
propItem.Len.ToString() +
" bytes", font1, blackBrush, X, Y);
Y += font1.Height;
count += 1;
}
font1.Dispose();
}
catch (Exception)
{
MessageBox.Show("There was an error." +
"Make sure the path to the image file is valid.");
}
}在我的表单应用程序中,我一直试图弄清楚如何调用这个函数。
private void metadataButton_Click(object sender, EventArgs e)
{
ExtractMetaData(e);
}但这正是我不成功的地方,我找出了它希望我传递的内容,包括不尝试任何ExtractMetaData();
长期而言,我希望读取并纠正图像文件中的特定元数据,但我正在使用这个示例来启动MSDN。
链接到元数据引用表
发布于 2017-02-23 19:29:35
此代码用于从画图事件处理程序调用此代码。它使用当时可用的Graphics对象直接绘制到显示器上。就像这样:
private void Form1_Paint(object sender, PaintEventArgs e)
{
ExtractMetadata(e);
}事件处理程序通常添加到Visual设计器中。但是它会将这一行添加到Designer.cs文件中:
private void InitializeComponent()
{
...
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
...
}您还可以在构造函数中手动添加这一行代码(在调用InitializeComponent之后)
https://stackoverflow.com/questions/42423985
复制相似问题