zoukankan      html  css  js  c++  java
  • 图片反转

    图片反转:

                if (_srcBitmap != null) //_srcBitmap = (Bitmap)Image.FromFile(_curFileName)
                {
                    _dstBitmap = (Bitmap)_srcBitmap.Clone();
                    BitmapData bmpData = _srcBitmap.LockBits(new Rectangle(0, 0, _srcBitmap.Width, _srcBitmap.Height),ImageLockMode.ReadWrite, _srcBitmap.PixelFormat);
                    BitmapData dstData = _dstBitmap.LockBits(new Rectangle(0, 0, _dstBitmap.Width, _dstBitmap.Height),ImageLockMode.ReadWrite, _dstBitmap.PixelFormat);
                    unsafe
                    {
                        byte* p = (byte*)bmpData.Scan0;
                        for (int y = 0; y < bmpData.Height; y++)
                        {
                            for (int x = 0; x < bmpData.Width; x++)
                            {
                                byte* dstp = (byte*)dstData.Scan0 + y * dstData.Stride + x * 3;
                                dstp[0] = p[bmpData.Width * 3 - (x + 1) * 3];
                                dstp[1] = p[bmpData.Width * 3 - (x + 1) * 3 + 1];
                                dstp[2] = p[bmpData.Width * 3 - (x + 1) * 3 + 2];
                            }
                            p += bmpData.Stride;    //Stride 为扫描宽度 Stride > bmpData.Width * 3 ,Width 表示每行的像素个数,每个像素占3个byte位
                        }
                        _srcBitmap.UnlockBits(bmpData);
                        _dstBitmap.UnlockBits(dstData);
                    }
                    _srcBitmap = (Bitmap)_dstBitmap.Clone();
                    Invalidate();
                }
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                if (_srcBitmap != null)
                {
                    g.DrawImage(_srcBitmap, 10, 30, _srcBitmap.Width, _srcBitmap.Height);
                }
            }

    伪彩色图片:

            private Bitmap gcTrans(Bitmap bmp, bool method, byte seg)
            {
                if (bmp != null)
                {
                    if (System.Drawing.Imaging.PixelFormat.Format24bppRgb == bmp.PixelFormat)
                    {
                        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                        IntPtr ptr = bmpData.Scan0;
                        int bytes = bmp.Width * bmp.Height * 3;
                        byte[] grayValues = new byte[bytes];
                        System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
                        bmp.UnlockBits(bmpData);
                        byte[] rgbValues = new byte[bytes];
                        //清零
                        Array.Clear(rgbValues, 0, bytes);
                        byte tempB;
                        if (method == false)
                        {
                            //强度分层法
                            for (int i = 0; i < bytes; i += 3)
                            {
                                byte ser = (byte)(256 / seg);
                                tempB = (byte)(grayValues[i] / ser);
                                //分配任意一种颜色
                                rgbValues[i + 1] = (byte)(tempB * ser);
                                rgbValues[i] = (byte)((seg - 1 - tempB) * ser);
                                rgbValues[i + 2] = 0;
                            }
                        }
                        else
                        {
                            //灰度级-彩色变换法
                            for (int i = 0; i < bytes; i += 3)
                            {
                                if (grayValues[i] < 64)
                                {
                                    rgbValues[i + 2] = 0;
                                    rgbValues[i + 1] = (byte)(4 * grayValues[i]);
                                    rgbValues[i] = 255;
                                }
                                else if (grayValues[i] < 128)
                                {
                                    rgbValues[i + 2] = 0;
                                    rgbValues[i + 1] = 255;
                                    rgbValues[i] = (byte)(-4 * grayValues[i] + 2 * 255);
                                }
                                else if (grayValues[i] < 192)
                                {
                                    rgbValues[i + 2] = (byte)(4 * grayValues[i] - 2 * 255);
                                    rgbValues[i + 1] = 255;
                                    rgbValues[i] = 0;
                                }
                                else
                                {
                                    rgbValues[i + 2] = 255;
                                    rgbValues[i + 1] = (byte)(-4 * grayValues[i] + 4 * 255);
                                    rgbValues[i] = 0;
                                }
                            }
                        }
                        bmp = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                        ptr = bmpData.Scan0;
                        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
                        bmp.UnlockBits(bmpData);
                        return bmp;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }

    源码下载

  • 相关阅读:
    感想篇:4)越来越精简的机械设计
    标准结构篇:5)热(散热)设计
    标准结构篇:4)EMC电磁兼容
    标准结构篇:2)O型橡胶密封圈
    进阶篇:1)制造发展阶段与对设计的要求
    标准结构篇:1)选用标准化的结构
    高阶篇:8.2)注塑模具讨论要点(讨模评审)
    基础篇:2.1)设计的深度-最小特征
    高阶篇:8.3)塑胶件试模
    高阶篇:8)注射模具开模流程总章
  • 原文地址:https://www.cnblogs.com/ytwy/p/5515773.html
Copyright © 2011-2022 走看看