zoukankan      html  css  js  c++  java
  • img

    public BitmapImage BitmapToImage(System.Drawing.Bitmap bitmap)
    {
    System.Drawing.Bitmap bitmapSource = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height);
    int i, j;
    for (i = 0; i < bitmap.Width; i++)
    for (j = 0; j < bitmap.Height; j++)
    {
    System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
    bitmapSource.SetPixel(i, j, newColor);
    }
    MemoryStream ms = new MemoryStream();
    bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
    bitmapImage.EndInit();

    return bitmapImage;
    }

    public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
    {
    BitmapSource returnSource;
    try
    {
    returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
    catch
    {
    returnSource = null;
    }
    return returnSource;
    }

    public System.Drawing.Bitmap WpfBitmapSourceToBitmap(BitmapSource s)
    {
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(s.PixelWidth, s.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    s.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
    bmp.UnlockBits(data);
    return bmp;
    }

  • 相关阅读:
    error MSB8031(将vs2010的工程用vs2013打开时出的错)
    MFC如何使控件大小随着对话框大小自动调整
    基于MFC对话框程序中添加菜单栏 (CMenu)
    mfc改变对话框窗口大小
    MFC设置对话框大小
    uart与usart区别
    uart接口介绍和认识
    USB引脚属性
    使用百度云服务器BCC搭建网站,过程记录
    linux下文件的复制、移动与删除命令为:cp,mv,rm
  • 原文地址:https://www.cnblogs.com/Logan626/p/5458660.html
Copyright © 2011-2022 走看看