旋转图像的最快方法是什么?有一些looooong和starving算法可以做到这一点,但到目前为止我还没有想出或发现任何特别快的东西。最快的方法是使用不安全的调用,直接用LockBits操作图像内存。这听起来很可怕,但它非常简单。如果您搜索LockBits,您会找到许多示例,例如此处。有趣的是:BitmapDataoriginalData=originalBitmap.LockBits(newRectangle(0,0,originalWidth,originalHeight),ImageLockMode.ReadOnly,PixelFormat.Format32bppRgb);获得BitmapData后,您可以传递像素并将它们映射到新图像(再次使用LockBits)。这比使用图形API快得多。这是我最后做的事情(经过大量的持续研究,以及TheCodeKing提供的有用链接):publicImageRotateImage(Imageimg,floatrotationAngle){//将返回的图像绘制到表单时,通过//修改你的点(-(img.Width/2)-1,-(img.Height/2)-1)绘制实际坐标。//创建一个空的位图图像Bitmapbmp=newBitmap((img.Width*2),(img.Height*2));//将Bitmap转换为Graphics对象Graphicsgfx=Graphics.FromImage(bmp);//将点系统原点设置为图像的中心gfx.TranslateTransform((float)bmp.Width/2,(float)bmp.Height/2);//现在旋转图像gfx.RotateTransform(rotationAngle);//将点系统原点移回0,0gfx.TranslateTransform(-(float)bmp.Width/2,-(float)bmp.Height/2);//将InterpolationMode设置为HighQualityBicubic以确保在转换为指定大小后获得高质量的图像gfx.InterpolationMode=InterpolationMode.HighQualityBicubic;//将我们的新图像绘制到图形对象上等其中心在旋转中心gfx.DrawImage(img,newPointF((img.Width/2),(img.Height/2)));//处理我们的图形对象gfx.Dispose();//返回图像returnbmp;干杯!voidGraphics.RotateTransform(浮动角度);这应该在C#中旋转图像它有什么作用?我没有对GDI+做过太多的试验。记住在绘制图像后反转旋转。这个答案返回它应该绘制的偏移量和已经旋转的图像。它的工作原理是将新图像重建为应该没有裁剪角的大小。最初由Hisenburg在#C#IRC聊天室和Bloodyaugust中编写。C#学习教程就是这样:在没有GDI+裁剪边缘的情况下旋转图像的最快方法是什么?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicstaticdoubleNormalizeAngle(doubleangle){doubledivision=angle/(Math.PI/2);双分数=Math.Ceiling(division)-除法;返回(分数*Math.PI/2);}publicstaticTupleRotateImage(Imageimg,doublerotationAngle){doublenormalizedRotationAngle=NormalizeAngle(rotationAngle);双宽度D=img.Width,heightD=img.Height;双newWidthD,newHeightD;newWidthD=Math.Cos(normalizedRotationAngle)*widthD+Math.Sin(normalizedRotationAngle)*heightD;newHeightD=Math.Cos(normalizedRotationAngle)*heightD+Math.Sin(normalizedRotationAngle;)*heightD+Math.Sin(normalizedRotationAngle;)*widthDintnewWidth,newHeight;newWidth=(int)Math.Ceiling(newWidthD);newHeight=(int)Math.Ceiling(newHeightD);尺寸偏移量=新尺寸((新宽度-img.Width)/2,(newHeight-img.Height)/2);位图bmp=newBitmap(newWidth,newHeight);图形gfx=Graphics.FromImage(bmp);//gfx.Clear(Color.Blue);gfx.TranslateTransform((float)bmp.Width/2,(float)bmp.Height/2);gfx.RotateTransform((float)(rotationAngle/Math.PI*180));gfx.TranslateTransform(-(float)bmp.Width/2,-(float)bmp.Height/2);gfx.InterpolationMode=InterpolationMode.HighQualityBicubic;gfx.DrawImage(img,newPointF((bmp.Width/2-img.Width/2),(bmp.Height/2-img.Height/2)));gfx.Dispose();返回新元组(bmp,偏移量);}System.Drawing.ImageimageToRotate=System.Drawing.Image.FromFile(imagePath);switch(rotationAngle.Value){case"90":imageToRotate.RotateFlip(RotateFlipType.Rotate90FlipNone);休息;案例“180”:imageToRotate.RotateFlip(RotateFlipType.Rotate180FlipNone);休息;案例“270”:imageToRotate.RotateFlip(RotateFlipType.Rotate270FlipNone);休息;default:thrownewException("不支持旋转角度。");}imageToRotate.Save(imagePath,ImageFormat.Jpeg);本文来自网络收集,不代表立场,如涉及侵权,请点击右侧联系管理员删除,如需转载请注明出处:
