【问题标题】:How to Increase the quality of the image after conversion from bytes?从字节转换后如何提高图像质量?
【发布时间】:2011-01-21 07:41:06
【问题描述】:

我在 wpf 应用程序中有 tiff 图像。首先我使用内存流将图像转换为字节,然后将字节转换为位图图像。我给图像提供了位图图像。我还拍摄了另一张图像并直接给它的源图像路径而不进行转换。我观察到的是转换后图像的质量变低了。为什么会发生?

我的代码如下。 我用了 File.ReadAllBytes("filepath") 将图像转换为字节。

我使用下面的方法从 byte[] 中获取 BitmapSource。然后我将 bitmapsource 分配给图像

    public static System.Windows.Media.Imaging.BitmapSource ConvertBytesToBitmapSource(byte[] imageBytes)

{
            System.Drawing.Bitmap source = new System.Drawing.Bitmap(ConvertBytesToImage(imageBytes));

            IntPtr imagePtr = source.GetHbitmap();

            System.Windows.Media.Imaging.BitmapSource bitmapSour = null;

            try
            {

                bitmapSour = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(imagePtr,

                  IntPtr.Zero, System.Windows.Int32Rect.Empty,

                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            }

            catch (Exception ex)
            {

                logger.Error("Error in converting bytes to BitmapSource", ex);
                throw ex;
            }

            finally
            {
                DeleteObject(imagePtr);
            }

            return bitmapSour;

        }

【问题讨论】:

  • 这是个好问题。如果没有压缩算法,除了伽玛校正,我看不出它们会如何降低质量。

标签: wpf image


【解决方案1】:

感谢 Waqas Raja 的回复。我解决了这个问题 将项目从 3.5 版本转换到 4.0 版本时,图像质量下降。在 3.5 版本中,默认图像质量是高质量的。但是在 4.0 版本中,我们需要明确指定图像的质量才能通过使用获得高质量BitScalingMode Dependency 属性为

RenderOptions.SetBitmapScalingMode(MyImage, BitmapScalingMode.HighQuality);

你可以参考链接

http://msdn.microsoft.com/en-us/library/bb613591.aspx

【讨论】:

    【解决方案2】:

    您应该使用 Graphics 对象来绘制新图像,而不是仅仅创建图像。这是示例代码

    System.Drawing.Image sourceImage = System.Drawing.Image.FromStream(sourceStrm, true, true); System.Drawing.Bitmap destBitmap = new System.Drawing.Bitmap(destWidth, destHeight); System.Drawing.Graphics grap = System.Drawing.Graphics.FromImage((System.Drawing.Image)destBitmap); grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; grap.DrawImage(sourceImage, 0, 0, destWidth, destHeight);

    如果不是您使用的方式,请您发布一些您使用的示例代码。

    【讨论】:

    • 你用的是什么方法,能不能提供几行代码
    • 我将应用程序从 3.5 版本转换为 4.0 版本时遇到了这个问题
    • 试试 System.Drawing.Graphics 而不是直接传递给位图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多