none
INSERT PICTURE FROM PROJECT FOLDER on event click

    Question

  • how do i insert a picture from my project folder on an EVENT CLICK .?

     

    myBitmapImage.UriSource = System.Windows.Application.GetResourceStream(new Uri("pic/diary emoticons/102.gif", UriKind.Relative));

     

    Error 1 Cannot implicitly convert type 'System.Windows.Resources.StreamResourceInfo' to 'System.Uri' 

    Thursday, March 13, 2008 3:12 AM

Answers

  • Here is what you can do if the picture is a resource

     

                Image imgCtrl = new Image();

                BitmapImage bmp = new BitmapImage();

                bmp.BeginInit();

                bmp.StreamSource = Application.GetResourceStream(new Uri("pic.jpg", UriKind.RelativeOrAbsolute)).Stream;

                bmp.EndInit();

                imgCtrl.Source = bmp;

     

                InlineUIContainer iu = new InlineUIContainer(imgCtrl);

                TextPointer pos = richBox.CaretPosition;

                pos.Paragraph.Inlines.Add(iu);

     

    and if not a resource, but available in the output folder

     

                Image imgCtrl = new Image();

                byte[] bt = File.ReadAllBytes("pic.jpg");

                MemoryStream strm = new MemoryStream(bt);

                BitmapImage bmp = new BitmapImage();

                bmp.BeginInit();

                bmp.StreamSource = strm;

                bmp.EndInit();

                imgCtrl.Source = bmp;

     

                InlineUIContainer iu = new InlineUIContainer(imgCtrl);

                TextPointer pos = richBox.CaretPosition;

                pos.Paragraph.Inlines.Add(iu);

    Thursday, March 13, 2008 10:49 AM
  • After loading the picture in the image, you can set the width and height of the control as per the size of the picture

     

                imgCtrl.Width = imgCtrl.Source.Width;

                imgCtrl.Height = imgCtrl.Source.Height; ;

     

    Friday, March 14, 2008 6:23 AM

All replies

  • you could do something like this (where img is the Image Control of your Window)

     

     

                img.BeginInit();

                BitmapImage bmp = new BitmapImage(new Uri("pic/mypic.jpg", UriKind.Relative));

                bmp.CacheOption = BitmapCacheOption.OnLoad;

                img.Source = bmp;

                img.EndInit();

    Thursday, March 13, 2008 3:56 AM
  • hi i tried this below.. but it cant get any pic out to my richtextbox  ..

     

    TextPointer tp = diary_richTextBox.CaretPosition;

     

    System.Windows.Controls.Image img = new System.Windows.Controls.Image();

    BitmapImage myBitmapImage = new BitmapImage();

    myBitmapImage.BeginInit();

    myBitmapImage.UriSource = new Uri("pic/diary emoticons/102.gif", UriKind.RelativeOrAbsolute);

    myBitmapImage.EndInit();

    img.Source = myBitmapImage;

     

    InlineUIContainer iu = new InlineUIContainer(img);

    tp.Paragraph.Inlines.Add(iu);

    Thursday, March 13, 2008 9:38 AM
  • Here is what you can do if the picture is a resource

     

                Image imgCtrl = new Image();

                BitmapImage bmp = new BitmapImage();

                bmp.BeginInit();

                bmp.StreamSource = Application.GetResourceStream(new Uri("pic.jpg", UriKind.RelativeOrAbsolute)).Stream;

                bmp.EndInit();

                imgCtrl.Source = bmp;

     

                InlineUIContainer iu = new InlineUIContainer(imgCtrl);

                TextPointer pos = richBox.CaretPosition;

                pos.Paragraph.Inlines.Add(iu);

     

    and if not a resource, but available in the output folder

     

                Image imgCtrl = new Image();

                byte[] bt = File.ReadAllBytes("pic.jpg");

                MemoryStream strm = new MemoryStream(bt);

                BitmapImage bmp = new BitmapImage();

                bmp.BeginInit();

                bmp.StreamSource = strm;

                bmp.EndInit();

                imgCtrl.Source = bmp;

     

                InlineUIContainer iu = new InlineUIContainer(imgCtrl);

                TextPointer pos = richBox.CaretPosition;

                pos.Paragraph.Inlines.Add(iu);

    Thursday, March 13, 2008 10:49 AM
  • Thanks.. great help. it works...

     

    but once inserted.. the picture size  become so big.. size distorted.. how do i maintain the original size of the picture ?

    Thursday, March 13, 2008 1:20 PM
  • hi.. any idea to maintain back to set the picture back certain width or back to original ?

    Friday, March 14, 2008 5:55 AM
  • After loading the picture in the image, you can set the width and height of the control as per the size of the picture

     

                imgCtrl.Width = imgCtrl.Source.Width;

                imgCtrl.Height = imgCtrl.Source.Height; ;

     

    Friday, March 14, 2008 6:23 AM
  • Bravo to Atual Gupta !!!  i tried to search for tutorial on this for a damn long time.. huhhh.. good saviour..

     

    bro..  i got another problem on insert that image into between the words .. it always insert that image on the last word ?

    i tried below but fail..

    InlineUIContainer iu = new InlineUIContainer(imgCtrl);

    TextPointer pos = diary_richTextBox.CaretPosition;

    pos.Paragraph.Inlines.Add(iu);

     

    --------------------------------------------------------

    something like this /??

    TextRange yl = new TextRange(diary_richTextBox.Selection.Start,diary_richTextBox.Selection.End)

     

     

    REALLY APRECIATE your effort . thanks again

     

     

    Friday, March 14, 2008 1:48 PM
  •  alainkkk wrote:

     

    REALLY APRECIATE your effort . thanks again

     

    Thanks

     

    I guess the issue with CaretPosition logic is that it will work if the richtextbox has focus. If your image is inserted by clicking on say a button, the focus would have shifted to the button and hence the caret postion will not help.

     

    My take is that you will need to parse through the Paragraph and then insert the image at appropriate place. I don't have access to my laptop where I was working on this sample so can't post a sample code here right now. Will try and do it later

    Sunday, March 16, 2008 4:46 AM
  • hahha.. bro.. good to hear from u .. any help for the question .. thanks  man

    Monday, March 17, 2008 5:15 PM
  • didn't get time to dig into this today.. let me do that tomorrow and revert

     

    Monday, March 17, 2008 5:38 PM
  • Snice I don't your exact need, here is what i did. I had a RichTextBox with two paragraphs set in XAML

     

    Code Snippet

            <RichTextBox x:Name="richBox" >

                <FlowDocument>

                    <Paragraph><Run Text="This is first paragraph"/></< SPAN>Paragraph>

                    <Paragraph><Run Text="This is second paragraph"/></< SPAN>Paragraph>

                </< SPAN>FlowDocument>

            </< SPAN>RichTextBox>

     

     

    Then in code behind, I inserted the picture as a new paragraph between these two as below

     

    Code Snippet

                Paragraph p = new Paragraph();

                p.Inlines.Add(new InlineUIContainer(imgCtrl));

     

                BlockCollection blocks = richBox.Document.Blocks;

                blocks.InsertAfter(blocks.FirstBlock, p);

     

     

    The logic of creating the image control is not include here. If you want to insert it between words, you can parse the Blocks. Each block is essentially a Paragraph. You can cast the block to Paragraph, then access the inline collection and get access to individual lines and then do the needful.

     

    Hope this helps.  

    Tuesday, March 18, 2008 4:16 AM