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

  • 相关阅读:
    mysql数据库主从同步复制原理
    NoSQL
    Mysqldump参数大全
    MySQL Show命令的使用
    学习shell脚本之前的基础知识
    详解MySQL大表优化方案
    sql索引的优缺点
    [C#] 取得每月第一天和最後一天、某月总天数
    Easy ui DateBox 控件格式化显示操作
    StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试
  • 原文地址:https://www.cnblogs.com/Logan626/p/5458660.html
Copyright © 2011-2022 走看看