none
img insert and save RRS feed

  • Question

  • hi all

    i have a image box in my win

    and i want to open any picture into and save it

    and change the picture any time i want 

    like the open dialog and save dialog

    tnks

    Thursday, May 16, 2019 10:36 AM

Answers

  • hi all

    i have a image box in my win

    and i want to open any picture into and save it

    and change the picture any time i want 

    like the open dialog and save dialog

    tnks

    Hi aninam,

    According to your description, you want to open picture in image control by openfiledialog, then saving this picture in file by savefiledialog, am I right? if yes, I do one sample that you can take a look:

     <StackPanel>
            <Image
                Name="image1"
                Width="300"
                Height="300"
                Margin="20" />
            <StackPanel Orientation="Horizontal">
                <Button
                    Name="btn1"
                    Width="200"
                    Height="30"
                    Margin="20"
                    Click="Btn1_Click"
                    Content="open file" />
                <Button
                    Name="btn2"
                    Width="200"
                    Height="30"
                    Click="Btn2_Click"
                    Content="save file" />
            </StackPanel>
    
        </StackPanel>
     public partial class Window26 : Window
        {
            public Window26()
            {
                InitializeComponent();
            }
            string filepath;
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                
                OpenFileDialog open = new OpenFileDialog();
                open.Multiselect = false;
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                bool? result = open.ShowDialog();
    
                if (result == true)
                {
                    filepath = open.FileName; // Stores Original Path in Textbox    
                    ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
                    image1.Source = imgsource;
                }
            }
    
            private void Btn2_Click(object sender, RoutedEventArgs e)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = "outputimage";
                dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
                if (dlg.ShowDialog() == true)
                {
                    var uri = new Uri(filepath);
                    var bitmap = new BitmapImage(uri);
    
                    // Save to file.
                    var encoder = new JpegBitmapEncoder(); // Or any other, e.g. PngBitmapEncoder for PNG.
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
                    encoder.QualityLevel = 100; // Set quality level 1-100.
    
                    using (var stream = dlg.OpenFile())
                    {
                        encoder.Save(stream);
                    }
                }
    
                
            }
        }

    If my reply has solved your issue, please remember to close your thread by clicking "mark as answer" below my reply, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by aminam Saturday, May 18, 2019 12:36 PM
    Friday, May 17, 2019 5:32 AM
    Moderator

All replies

  • hi all

    i have a image box in my win

    and i want to open any picture into and save it

    and change the picture any time i want 

    like the open dialog and save dialog

    tnks

    Hi aninam,

    According to your description, you want to open picture in image control by openfiledialog, then saving this picture in file by savefiledialog, am I right? if yes, I do one sample that you can take a look:

     <StackPanel>
            <Image
                Name="image1"
                Width="300"
                Height="300"
                Margin="20" />
            <StackPanel Orientation="Horizontal">
                <Button
                    Name="btn1"
                    Width="200"
                    Height="30"
                    Margin="20"
                    Click="Btn1_Click"
                    Content="open file" />
                <Button
                    Name="btn2"
                    Width="200"
                    Height="30"
                    Click="Btn2_Click"
                    Content="save file" />
            </StackPanel>
    
        </StackPanel>
     public partial class Window26 : Window
        {
            public Window26()
            {
                InitializeComponent();
            }
            string filepath;
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                
                OpenFileDialog open = new OpenFileDialog();
                open.Multiselect = false;
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                bool? result = open.ShowDialog();
    
                if (result == true)
                {
                    filepath = open.FileName; // Stores Original Path in Textbox    
                    ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
                    image1.Source = imgsource;
                }
            }
    
            private void Btn2_Click(object sender, RoutedEventArgs e)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = "outputimage";
                dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
                if (dlg.ShowDialog() == true)
                {
                    var uri = new Uri(filepath);
                    var bitmap = new BitmapImage(uri);
    
                    // Save to file.
                    var encoder = new JpegBitmapEncoder(); // Or any other, e.g. PngBitmapEncoder for PNG.
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
                    encoder.QualityLevel = 100; // Set quality level 1-100.
    
                    using (var stream = dlg.OpenFile())
                    {
                        encoder.Save(stream);
                    }
                }
    
                
            }
        }

    If my reply has solved your issue, please remember to close your thread by clicking "mark as answer" below my reply, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by aminam Saturday, May 18, 2019 12:36 PM
    Friday, May 17, 2019 5:32 AM
    Moderator
  • thnks 

    its so nice and helpfuly

    but i forgot to add somthing

    i need to save this loaded picture to my database in a folder made by same window

    and this all is in my wpf app

    best regards

    Saturday, May 18, 2019 12:36 PM