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;
    }

  • 相关阅读:
    JAVA 桥接模式
    字模生成/提取原理
    const修饰指针
    BMP格式分析
    [转载]在.Net中使用SMTP发送邮件
    [转载]MD5加密解密
    四十二。java
    四十四。java
    四十一。复习第十二章内容
    三十六。文件流
  • 原文地址:https://www.cnblogs.com/Logan626/p/5458660.html
Copyright © 2011-2022 走看看