本文介绍了图像缩放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 这可能是一个简单的错误。但是,我一直在研究这个问题几个小时,看不出有什么问题。 我想通过比例因子进行缩放和成像。请查看以下代码。 < StackPanel > < StackPanel 名称 = StackDesktop 宽度 = 自动 高度 = 自动 > < 标签 名称 = LabSlideName 宽度 = 自动 内容 = 幻灯片名称 高度 = 25 前景 = 白色 / > < 网格 名称 = GridPreview 宽度 = 640 高度 = 480 HorizontalAlignment = 中心 VerticalAlignment = 中心 已移除 = DarkBlue > < 网格 名称 = GridImage > < Viewbox 名称 = ViewBoxPreview 拉伸 = 无 StretchDirection = 两者 / > < / Grid > < / Grid > < / StackPanel > < / StackPanel > 代码背后 SlideShow.SlideShow node = ExperimentLibrary.Experiment.Instance.GetSlideShow()。GetNodeByID(id); System.Windows.Controls.Image img = new System.Windows.Controls.Image(); 位图bmp = new Bitmap((int)node.Slide.PresentationSize.Width,(int)node.Slide.PresentationSize.Height); 图形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; 原始图片尺寸为640 * 480。最终显示的图像是234 * 135。由于比例为0.4685,GridImage.Width = 299,GridImage.Height = 224,我期待最终图像为299 * 224. 有人可以帮助吗因为我被卡住了... 干杯.... 解决方案 [这不是回答,应该删除。 OP应该使用改善问题将其移至问题--SA] 看这里, < StackPanel > ; < StackPanel 名称 = StackDesktop 宽度 = 自动 高度 = 自动 > < 标签 名称 = LabSlideName 宽度 = 自动 内容 = 幻灯片名称 高度 = 25 前景 = 白色 / > < 网格 名称 = GridPreview 宽度 = 640 Height = 480 HorizontalAlignment = 中心 VerticalAlignment = 中心 背景 = DarkBlue 保证金 = 26, 0,34,0 > < 网格 名称 = GridImage > < / Grid > < / Grid > < / StackPanel > < / StackPanel > SlideShow.SlideShow node = ExperimentLibrary.Experiment。 。Instance.GetSlideShow()GetNodeByID(ID); System.Windows.Controls.Image img = new System.Windows.Controls.Image(); 位图bmp = new Bitmap((int)node.Slide.PresentationSize.Width,(int)node.Slide.PresentationSize.Height); 图形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); 图像仍然是234 * 135 ...任何想法为什么? ? HiThis 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 BehindSlideShow.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.... 解决方案 [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??? 这篇关于图像缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 22:52