开发者

How do I fill a rectangle with an ImageBrush using a file from the hard drive

I am trying to fill a rectangle with a file that I have saved to the hard drive. I know that I have to use a ImageBrush and I think I know how to do that if the image is an included resource. In this case the file is created and setting 开发者_开发知识库on the hard drive but when I try to use it with the code below the rectangle changes but it changes to show the form back color not the image as I had expected (almost as if the image is invisible).

    using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog"))
    {
        dynamic imageFile = CommonDialog.ShowAcquireImage();
        if (imageFile != null)
        {
            string filePath = string.Format("d:\\{0}.jpg", Guid.NewGuid());
            imageFile.SaveFile(filePath);

            rectangle2.Fill = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri(filePath, UriKind.Absolute))
            };
        }
    }

Update: I was able to get this to work by replacing the code block inside the If Then with the following:

            {
                string filePath = string.Format("d:\\{0}.jpg", Guid.NewGuid());
                imageFile.SaveFile(filePath);

                BitmapImage bitmapBase = new BitmapImage();
                dynamic fileData = imageFile.FileData;
                byte[] imageData = fileData.BinaryData;
                MemoryStream ms = new MemoryStream(imageData);
                bitmapBase.SetSource(ms);
                WriteableBitmap writableBitmap = new WriteableBitmap(bitmapBase);

                rectangle2.Fill = new ImageBrush() { ImageSource = (writableBitmap) };

            }


When using Silverlight 4 you can create an out-of-browser application thatt can access (part of) the local disk.

See here how


You cannot access images, or any files, from a local drive (except for Isolated storage or a stream from a file open dialog). These are all security measures.

As you did not mention Out Of Browser I assume this is just a web/client app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜