C# Windows Forms - How to programmatically set image preview for file folder (VSTO 2010)

限于喜欢 提交于 2019-12-11 08:54:28

问题


Windows Forms - VSTO - Outlook

Background - I am creating a digital archive add-in for Office where the user can search the database for the client (whom the document belongs to) and it will save the file to the appropriate folder(s) based on the nature of the file. So far this is working for Word as planned but I am now using Outlook which has more to consider (attachments, message body, etc.).

I have got it working so far that the attachments are saved into a temporary folder (which is emptied each time the windows form closes) ready to be sorted and I can obtain information about sender/subject/email body. The list of attachments is set out into a CheckedListBox

Current Problem - When a user is looking to archive an attachment (a lot of documents/scanned documents will come up), images will be confusing as they may be necessary or entirely unimportant so I wish to preview the images.

I am trying to get it so on the event of

private void chkAttachments_SelectedValueChanged(object sender, EventArgs e)

The image shows in picAttachPreview (PictureBox) as a preview of that file. This will be taking the image from tempfolder (@"c:\temp\DigitalArchive").

I understand this is wrong but I am trying to set the source for the image shown on screen on that SelectedValueChanged event.

My [Incorrect] Code -

if(chkAttachments.Text.Contains(".jpg"))
        {
            var selectedImage = chkAttachments.SelectedValue.ToString();
            picAttachPreview.Image = tempfolder + @"\" + selectedImage; //(A)
        }

The (A) line is the issue and although I understand why, I don't know how to resolve it. The filepath is constructed with tempfolder and selectedImage (e.g. ScannedDoc.jpg) but the file path type is String but picAttachPreview is System.Drawing.Image so I assume I am looking at the wrong property of picAttachPreview to set the source of the image.

Any help or guidance will be immensely appreciated. Thank you.

(Also if you know of any good way I can set the same nature of preview for documents/PDF then I will be immensely grateful)

Edit Although the link solves part of my problem, there is an issue with chkAttachments.SelectedValue.ToString() which I answered below. (If anyone can advise me on the site etiquette for this situation. Do I delete the question or leave it with the answer I found so that people can find the solution to the same problem in future? Thank you)


回答1:


After some playing around, I found another problem (chkAttachment.Text works whereas .SelectedValue.ToString() does not.

Also the issue with the string to image format is resolved by prefixing the path with Image.FromFile(

So the correct way of changing the image upon selection is:

if(chkAttachments.Text.Contains(".jpg"))
        {
            var selectedImage = chkAttachments.Text;
            picAttachPreview.Image = Image.FromFile(tempfolder + @"\" + selectedImage);
        }


来源:https://stackoverflow.com/questions/38825962/c-sharp-windows-forms-how-to-programmatically-set-image-preview-for-file-folde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!