zoukankan      html  css  js  c++  java
  • 生成单色BMP图片

    在项目的属性里要勾选 容许不安全代码

     public Bitmap ConvertTo24bppTo1bpp(Bitmap SrcImg)

            {
                unsafe
                {
                    byte* SrcPointer, DestPointer;
                    int Width, Height, SrcStride, DestStride;
                    int X, Y, Index, Sum; ;
                    Bitmap DestImg = new Bitmap(SrcImg.Width, SrcImg.Height, PixelFormat.Format1bppIndexed);
                    BitmapData SrcData = new BitmapData();
                    SrcImg.LockBits(new Rectangle(0, 0, SrcImg.Width, SrcImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb, SrcData);
                    BitmapData DestData = new BitmapData();
                    DestImg.LockBits(new Rectangle(0, 0, SrcImg.Width, SrcImg.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed, DestData);
                    Width = SrcImg.Width; Height = SrcImg.Height; SrcStride = SrcData.Stride; DestStride = DestData.Stride;
                    for (Y = 0; Y < Height; Y++)
                    {
                        SrcPointer = (byte *)SrcData.Scan0 + Y * SrcStride;
                        DestPointer = (byte*)DestData.Scan0 + Y * DestStride;
                        Index = 7; Sum = 0;
                        for (X = 0; X < Width; X++)
                        {
                            if (*SrcPointer + (*(SrcPointer + 1) << 1) + *(SrcPointer + 2)>= 512) Sum += (1 << Index);
                            if (Index == 0)
                            {
                                *DestPointer = (byte)Sum;
                                Sum = 0;
                                Index = 7;
                                DestPointer++;
                            }
                            else
                                Index--;
                            SrcPointer+=3;
                        }
                        if (Index != 7) *DestPointer = (byte)Sum;
                    }
                    SrcImg.UnlockBits(SrcData);
                    DestImg.UnlockBits(DestData);
                    return DestImg;
                }
            }
  • 相关阅读:
    常见错误
    mac安装cocoapods
    WPS复制时删除超链接
    Network | 协议栈
    Leetcode | Remove Duplicates from Sorted Array I && II
    Leetcode | Search in Rotated Sorted Array I & II
    Leetcode | Jump Game I && II
    Leetcode | Combination Sum I && II
    Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
    Leetcode | Trapping Rain Water
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3080641.html
Copyright © 2011-2022 走看看