Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
XML
 <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
Updated 18-Apr-13 13:23pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Apr-13 0:34am    
WPF? Tag it.
—SA
FlurryKnox 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 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

[This is not an answer and should be removed. OP should move it to the question using "Improve question" —SA]

Look here,

XML
<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???
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900