关于PPC图片旋转功能【原创】

    我是一个剧懒的人,算算看最近一次上博客已经是去年的事了。唉,没办法,谁叫咱天生就懒呢。

    最近碰到一个要控制摄像头的代码,因为图片旋转的事,着实忙乎了我一阵子,网上看了无数代码,就是没有能在PPC上用的,没办法,只好自己动手重新写了个,今天贴出来,大家看看,有更好的改进意见,大家交流。

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Jrong
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Drawing
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 指定图像的旋转方向和用于翻转图像的轴。(自定义,较系统自带弱)
        
/// </summary>

        public enum RotateType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
            
/// 90 度旋转。
            
/// </summary>

            Rotate90 = 1
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
            
/// 180 度旋转。
            
/// </summary>

            Rotate180 = 2,
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
            
/// 270 度旋转。
            
/// </summary>

            Rotate270 = 4
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
旋转图片,直接内存操作,会只动释放输入图片#region 旋转图片,直接内存操作,会只动释放输入图片

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 旋转图片,直接内存操作,会自动释放输入图片(为节省内存)
        
/// </summary>
        
/// <param name="pBitmap">输入图片,计算完成后会直接 Dispose()(为节省内存)</param>
        
/// <param name="pRotateType">90、180 或是 270 度</param>
        
/// <returns></returns>

        public static System.Drawing.Bitmap RotateBitmap(System.Drawing.Bitmap pBitmap, RotateType pRotateType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            System.Drawing.Bitmap imgOut;

            
switch (pRotateType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
case RotateType.Rotate90:
                
case RotateType.Rotate270:
                    imgOut 
= new System.Drawing.Bitmap(pBitmap.Height, pBitmap.Width, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    
break;

                
case RotateType.Rotate180:
                    imgOut 
= new System.Drawing.Bitmap(pBitmap.Width, pBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    
break;

                
defaultreturn pBitmap;
            }


            System.Drawing.Imaging.BitmapData dataIn 
= pBitmap.LockBits(new System.Drawing.Rectangle(00, pBitmap.Width, pBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            System.Drawing.Imaging.BitmapData dataOut 
= imgOut.LockBits(new System.Drawing.Rectangle(00, imgOut.Width, imgOut.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            
unsafe
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
byte* pIn = (byte*)(dataIn.Scan0.ToPointer());
                
byte* pOut = (byte*)(dataOut.Scan0.ToPointer());

                
switch (pRotateType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
case RotateType.Rotate90:

                        pOut 
+= dataOut.Stride * (dataOut.Height - 1); // 定义到初始位置

                        
for (int y = 0; y < dataIn.Height; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int x = 0; x < dataIn.Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                pOut[
0= (byte)pIn[0];
                                pOut[
1= (byte)pIn[1];
                                pOut[
2= (byte)pIn[2];

                                pIn 
+= 3;
                                pOut 
-= dataOut.Stride;   // 最后一次无效,多减了一次
                            }

                            pIn 
+= dataIn.Stride - dataIn.Width * 3;
                            pOut 
+= dataOut.Stride * dataOut.Height + 3// 因为上面多 减 了一次,所以这里要加回来
                        }

                        
break;

                    
case RotateType.Rotate180:

                        pOut 
+= dataOut.Stride * dataOut.Height - 3// 定义到初始位置

                        
for (int y = 0; y < dataIn.Height; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int x = 0; x < dataIn.Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                pOut[
0= (byte)pIn[0];
                                pOut[
1= (byte)pIn[1];
                                pOut[
2= (byte)pIn[2];

                                pIn 
+= 3;
                                pOut 
-= 3;
                            }

                            pIn 
+= dataIn.Stride - dataIn.Width * 3;
                            pOut 
-= dataOut.Stride - dataOut.Width * 3;
                        }

                        
break;

                    
case RotateType.Rotate270:

                        pOut 
+= 3 * (dataOut.Width - 1); // 定义到初始位置

                        
for (int y = 0; y < dataIn.Height; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int x = 0; x < dataIn.Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                pOut[
0= (byte)pIn[0];
                                pOut[
1= (byte)pIn[1];
                                pOut[
2= (byte)pIn[2];

                                pIn 
+= 3;
                                pOut 
+= dataOut.Stride;   // 最后一次无效,多加了一次
                            }

                            pIn 
+= dataIn.Stride - dataIn.Width * 3;
                            pOut 
-= dataOut.Stride * dataOut.Height + 3;// 因为上面多 加 了一次,所以这里要 减 回来  -(-3) = +3
                        }

                        
break;
                }

            }


            imgOut.UnlockBits(dataOut);
            pBitmap.UnlockBits(dataIn);

            pBitmap.Dispose(); 
// 卸载输入图片资源,为了防止出现 System.OutOfMemoryException 错误
            pBitmap = null;

            
return imgOut;
        }


        
#endregion

    }

}
posted on 2007-10-26 10:46  Jrong 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jrong/archive/2007/10/26/938353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值