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

  • 相关阅读:
    抓取网页萃取网页内容的代码 选择自 liujien 的 Blog
    asp.net2.0
    C# veriosn 3
    ASP.NET常用代码
    vbs automation copy file X: to X
    教学进度
    八岁女孩打电话给爆破公司要求炸毁学校(带中文翻译)
    闲话排序问题
    奋进号(Endeavour)太空梭,将执行最后一次太空任务
    Chrome: Google Translate 开始支持语音输入了!
  • 原文地址:https://www.cnblogs.com/Logan626/p/5458660.html
Copyright © 2011-2022 走看看