久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

LockBits 圖像旋轉方法不起作用?

LockBits image rotation method not working?(LockBits 圖像旋轉方法不起作用?)
本文介紹了LockBits 圖像旋轉方法不起作用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

大家好.在厭倦了 Get/Set Pixel 和 RotateTransfom 的緩慢性能和古怪行為之后,我求助于使用 LockBits 進行 2d 位圖圖像旋轉.所以這是我想出的代碼,據我估計,它應該可以完美運行.沒有.

Hey all. I resorted to using LockBits for 2d bitmap image rotation after getting fed up with the slow performance and wacky behavior of both Get/Set Pixel, and RotateTransfom. So here is the code I've come up with, and by my reckoning, it should work perfectly. It doesn't.

private static void InternalRotateImage(Bitmap originalBitmap, Bitmap rotatedBitmap, PointF centerPoint, float theta)
    {
        BitmapData originalData = originalBitmap.LockBits(
            new Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height),
            ImageLockMode.ReadOnly,
            originalBitmap.PixelFormat);

        BitmapData rotatedData = rotatedBitmap.LockBits(
            new Rectangle(0, 0, rotatedBitmap.Width, rotatedBitmap.Height),
            ImageLockMode.WriteOnly,
            rotatedBitmap.PixelFormat);

        unsafe
        {
            byte[,] A = new byte[originalData.Height * 2, originalBitmap.Width * 2];
            byte[,] R = new byte[originalData.Height * 2, originalBitmap.Width * 2];
            byte[,] G = new byte[originalData.Height * 2, originalBitmap.Width * 2];
            byte[,] B = new byte[originalData.Height * 2, originalBitmap.Width * 2];

            for (int y = 0; y < originalData.Height; y++)
            {
                byte* row = (byte*)originalData.Scan0 + (y * originalData.Stride);
                for (int x = 0; x < originalData.Width; x++)
                {
                    B[y, x] = row[x * 4];
                    G[y, x] = row[x * 4 + 1];
                    R[y, x] = row[x * 4 + 2];
                    A[y, x] = row[x * 4 + 3];
                }
            }

            for (int y = 0; y < rotatedData.Height; y++)
            {
                byte* row = (byte*)rotatedData.Scan0 + (y * rotatedData.Stride);
                for (int x = 0; x < rotatedData.Width; x++)
                {
                    int newy = (int)Math.Abs((Math.Cos(theta) * (x - centerPoint.X) - Math.Sin(theta) * (y - centerPoint.Y) + centerPoint.X));
                    int newx = (int)Math.Abs((Math.Sin(theta) * (x - centerPoint.X) + Math.Cos(theta) * (y - centerPoint.Y) + centerPoint.Y));

                    row[x * 4] = B[newy, newx];
                    row[x * 4 + 1] = G[newy, newx];
                    row[x * 4 + 2] = R[newy, newx];
                    row[x * 4 + 3] = A[newy, newx];
                }
            }

        }
            originalBitmap.UnlockBits(originalData);
            rotatedBitmap.UnlockBits(rotatedData);
        }

有人有什么想法嗎?我剛出來.提前致謝!

Anybody got any ideas? I'm fresh out. Thanks in advance!

這就是我最終使用的(非常感謝 Hans Passant):

This is what I ended up using (many thanks to Hans Passant):

private Image RotateImage(Image img, float rotationAngle)
    {
        Image image = new Bitmap(img.Width * 2, img.Height * 2);
        Graphics gfx = Graphics.FromImage(image);

        int center = (int)Math.Sqrt(img.Width * img.Width + img.Height * img.Height) / 2;
        gfx.TranslateTransform(center, center);
        gfx.RotateTransform(rotationAngle);
        gfx.DrawImage(img, -img.Width / 2, -img.Height / 2);

        return image;
    }

這和他的一樣,只是基于每個圖像,而不是形式.

It's the same thing as his, just on a per image basis, as opposed to a form.

推薦答案

您正在為自己挖一個更深的坑.這很早就出錯了,旋轉位圖的大小不是寬 x 高.這也是非常低效的.您需要啟動 RotateTransform,同時使用 TranslateTransform 并選擇正確的圖像繪制位置也很重要.

You are digging yourself a deeper hole. This goes wrong early, the size of the rotated bitmap is not Width x Height. It is also very inefficient. You need to get RotateTransform going, it is important to also use TranslateTransform and pick the correct image drawing location.

這是一個示例 Windows 窗體應用程序,它圍繞其中心點旋轉位圖,偏移量剛好足以在窗體旋轉時接觸窗體的內邊緣.在表單上放置一個計時器并使用項目 + 屬性、資源選項卡添加圖像資源.將其命名為 SampleImage,它不必是方形的.使代碼看起來像這樣:

Here's a sample Windows Forms app that rotates a bitmap around its center point, offset just enough to touch the inner edge of the form when it rotates. Drop a Timer on the form and add an image resource with Project + Properties, Resource tab. Name it SampleImage, it doesn't have to be square. Make the code look like this:

public partial class Form1 : Form {
    private float mDegrees;
    private Image mBmp;
    public Form1() {
        InitializeComponent();
        mBmp = Properties.Resources.SampleImage;
        timer1.Enabled = true;
        timer1.Interval = 50;
        timer1.Tick += new System.EventHandler(this.timer1_Tick);
        this.DoubleBuffered = true;
    }
    private void timer1_Tick(object sender, EventArgs e) {
        mDegrees += 3.0F;
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e) {
        int center = (int)Math.Sqrt(mBmp.Width * mBmp.Width + mBmp.Height * mBmp.Height) / 2;
        e.Graphics.TranslateTransform(center, center);
        e.Graphics.RotateTransform(mDegrees);
        e.Graphics.DrawImage(mBmp, -mBmp.Width/2, -mBmp.Height/2);
    }
}

您可以通過創建 32bppPArgb 格式的位圖來加快繪制速度,我跳過了這一步.

You can make draw a lot faster by creating a bitmap in the 32bppPArgb format, I skipped that step.

這篇關于LockBits 圖像旋轉方法不起作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 欧美精品二区三区四区免费看视频 | 日韩一级黄色片 | 亚洲综合日韩 | 亚洲精品在线视频观看 | 久久精品在线 | 午夜aaa| 国产又粗又黄又爽又硬的视频 | 日本中文字幕在线 | 成人高清视频在线观看 | 中文字幕1区 | wwww黄色| www午夜| 色污污 | 在线观看av免费 | 久久午夜视频 | 日本高清视频网站 | 久久视频这里只有精品 | 久久精品在线 | 成人高潮片免费 | 色婷婷狠狠 | 欧美三级在线看 | 亚洲性网站 | 久久中文网| 国产欧美精品一区二区色综合 | 亚洲福利一区 | 日韩高清精品免费观看 | 久久机热这里只有精品 | 精品欧美在线 | 日韩欧美中文 | 黄视频网站在线观看 | 97超碰在线免费观看 | 天天干天天操天天摸 | 一级免费看 | 亚洲综合图片区 | 欧产日产国产69 | 伊人综合影院 | 亚洲激情一区 | 99精品久久久久久 | 黄色成人在线 | 亚洲国产成人在线 | 黄色片网站视频 |