【问题标题】:Problem with converting from bitmap to ImageBitmap in c#在c#中从位图转换为ImageBitmap的问题
【发布时间】:2018-11-10 06:50:41
【问题描述】:

当我将两个像素位图转换为图像位图时,我将其更改为具有许多像素的普通图像并将其转换为渐变(黑色和白色像素现在从黑色到灰色渐变为白色)

public static BitmapImage Bitmap2BitmapImage(this Bitmap bitmap)
{
    using (var memory = new MemoryStream())
    {
        bitmap.Save(memory, ImageFormat.Png);
        memory.Position = 0;

        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memory;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
        bitmapImage.Freeze();

        return bitmapImage;
    }
}

【问题讨论】:

  • 那么你的问题是什么?您是否希望生成的 BitmapImage 也为 2 像素宽?如果是,您可以在 bitmapImage 上设置 DecodePixelWidth/DecodePixelHeight 属性。
  • 我不需要 2 个像素的图像,当我创建两个像素的位图时,一个像素的黑色第二个白色,然后我将它转换为 imagebitmap,我想要得到一半黑色一半白色的图像,但我得到了从黑色到白色的颜色渐变(你知道边界不清晰,颜色从一种平滑地变化到另一种)
  • 也许这会对你有所帮助:stackoverflow.com/questions/2381012/…
  • 您关注的是错误的问题。回到生成 Bitmap 对象的代码。用于生成它的 Graphics.InterpolationMode 和 Graphics.PixelOffsetMode 属性对于放大到更大尺寸的非常小的位图来说非常重要。也许是 Graphics.SmoothingMode,没什么可看的,很难分辨。
  • 那么如何关闭BitmapImage中的SmoothingMode呢?

标签: c# image


【解决方案1】:
public Bitmap getRandomTwo()
        {
            Bitmap alone = new Bitmap(1, 2);
            alone.SetPixel(0, 0, Color.White);
            alone.SetPixel(0, 1,Color.Black);
            return alone;
        }

这是生成位图的代码

【讨论】:

    【解决方案2】:

    更新: 这是一种无需平滑即可缩放Bitmap 的方法。

            public static Bitmap Scale(int count, Bitmap source)
        {
            if (count <= 0) { return source; }
            var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
            var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
                var srcByte = (byte*)sourcedata.Scan0.ToPointer();
                var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
                for (var y = 0; y < bitmapdata.Height; y++) {
                    for (var x = 0; x < bitmapdata.Width; x++) {
                        long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
                        dstByte[0] = srcByte[index];
                        dstByte[1] = srcByte[index + 1];
                        dstByte[2] = srcByte[index + 2];
                        dstByte[3] = srcByte[index + 3];
                        dstByte += 4;
                    }
                }
            }
            source.UnlockBits(sourcedata);
            bitmap.UnlockBits(bitmapdata);
    
            return bitmap;
        }
    

    在这种情况下,您可以通过以下方式获得正确的BitmapSource

       var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
       .ToBitmapSource();//Convert to bitmap source
    

    如果您只想将 Bitmap 转换为 BitmapSource 以显示在 WPF 控件中,请尝试以下方法:

        [DllImport("gdi32", EntryPoint = "DeleteObject")]
        public static extern bool DeleteHBitmap(IntPtr hObject);        
    
    
        public static BitmapSource ToBitmapSource(this Bitmap target)
        {
            var hbitmap = target.GetHbitmap();
            try {
                return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
                DeleteHBitmap(hbitmap);
            }
        }
    

    不要忽略 DeleteHBitmap 调用!

    【讨论】:

    • 更新,在转换成BitmapSource之前必须先缩放位图
    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多