Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 image
Hi
 
This is probably a simple mistake. However, I have been working on this for hours and can't see what is wrong.
 
I want to scale and image by a scaling factor. Look at the following code.
                     <StackPanel>
                        <StackPanel Name="StackDesktop" Width="Auto" Height="Auto" >
                            <Label Name="LabSlideName" Width="Auto" Content="Slide Name" Height="25" Foreground="White"/>
                            <Grid Name="GridPreview" Width="640" Height="480" HorizontalAlignment="Center" VerticalAlignment="Center" removed="DarkBlue" >
                                <Grid Name="GridImage">
                                    <Viewbox Name="ViewBoxPreview" Stretch="None" StretchDirection="Both" />
                                </Grid>
                            </Grid>
                        </StackPanel>
                    </StackPanel>
 
Code Behind
 
SlideShow.SlideShow node = ExperimentLibrary.Experiment.Instance.GetSlideShow().GetNodeByID(id);
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            Bitmap bmp = new Bitmap((int)node.Slide.PresentationSize.Width, (int)node.Slide.PresentationSize.Height);
             
            Graphics g = System.Drawing.Graphics.FromImage(bmp);
            node.Slide.Draw(g);
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            img.Source = WpfBitmap;
 
            double scale = GridPreview.Width / System.Windows.SystemParameters.PrimaryScreenWidth;
            GridImage.Width = (int)(WpfBitmap.Width * scale);
            GridImage.Height = (int)(WpfBitmap.Height * scale);
            ViewBoxPreview.Stretch = Stretch.UniformToFill; ;
            ViewBoxPreview.Child = img;
            
        
 
The original image size is 640*480. The final displayed image is 234*135. As scale is 0.4685, and GridImage.Width = 299 and GridImage.Height = 224, I was expecting the final image to be 299*224.
 
Can someone please help as I am stuck...
 
Cheers....
Posted 18-Apr-13 14:17pm
Edited 18-Apr-13 14:23pm
v2
Comments
Sergey Alexandrovich Kryukov at 19-Apr-13 0:34am
   
WPF? Tag it.
—SA
FlurryKnox at 19-Apr-13 3:00am
   
Yes, that's what I tried first. E.g. Change Width and Height and then Stretch to UniformToFill and StretchDirection to Both. The resulting image is still 234*135. E.g. the results are the same.???? I've tried about everything I can think of.
Sergey Alexandrovich Kryukov at 19-Apr-13 12:09pm
   
No. I did not ask you for clarification.
 
This question has tags. Add "WPF". Use "Improve question" above. You are the one who is interested in that.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

[This is not an answer and should be removed. OP should move it to the question using "Improve question" —SA]
 
Look here,
 
<StackPanel>
                        <StackPanel Name="StackDesktop" Width="Auto" Height="Auto" >
                            <Label Name="LabSlideName" Width="Auto" Content="Slide Name" Height="25" Foreground="White"/>
                            <Grid Name="GridPreview" Width="640" Height="480" HorizontalAlignment="Center" VerticalAlignment="Center" Background="DarkBlue" Margin="26,0,34,0" >
                                <Grid Name="GridImage">
 
                                </Grid>
                            </Grid>
                        </StackPanel>
                    </StackPanel>
 

SlideShow.SlideShow node = ExperimentLibrary.Experiment.Instance.GetSlideShow().GetNodeByID(id);
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            Bitmap bmp = new Bitmap((int)node.Slide.PresentationSize.Width, (int)node.Slide.PresentationSize.Height);
             
            Graphics g = System.Drawing.Graphics.FromImage(bmp);
            node.Slide.Draw(g);
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            img.Source = WpfBitmap;
 
            
                double scale = GridPreview.Width / System.Windows.SystemParameters.PrimaryScreenWidth;
                img.Width = (int)(WpfBitmap.Width * scale);
                img.Height = (int)(WpfBitmap.Height * scale);
                img.Stretch = Stretch.UniformToFill; ;
                img.StretchDirection = StretchDirection.Both;
                GridImage.Children.Add(img);
            
 
Image is still 234*135...Any ideas why???
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100