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

LockBits 圖像旋轉(zhuǎn)方法不起作用?

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

問題描述

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

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.

推薦答案

您正在為自己挖一個更深的坑.這很早就出錯了,旋轉(zhuǎn)位圖的大小不是寬 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 窗體應(yīng)用程序,它圍繞其中心點旋轉(zhuǎn)位圖,偏移量剛好足以在窗體旋轉(zhuǎn)時接觸窗體的內(nèi)邊緣.在表單上放置一個計時器并使用項目 + 屬性、資源選項卡添加圖像資源.將其命名為 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);
    }
}

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

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

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

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

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應(yīng)用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應(yīng)用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應(yīng)用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 91免费观看| 狠狠骚| 精品中文字幕一区二区 | 九九综合 | 99re| 久久精片| 超碰最新在线 | 成人性视频免费网站 | www.日本在线播放 | 色伊人网| 国产你懂的在线观看 | 亚洲成人免费视频在线观看 | 久久久久久久久久久久久九 | 国产视频1区 | 亚洲一区二区国产 | 99热精品在线观看 | 久久9久| 色婷婷狠狠 | 拍戏被cao翻了h承欢 | 欧美一区免费 | 羞羞在线观看视频 | 国产精品久久久久久影院8一贰佰 | 欧美黑人一区 | 欧美日日 | 国产精品成人一区二区三区夜夜夜 | 午夜精品福利视频 | 欧洲成人午夜免费大片 | 久久久爽爽爽美女图片 | 国产成人精品一区二区三区在线 | 一二三在线视频 | 精品中文视频 | 69av在线视频 | 毛片免费在线 | 最近中文字幕第一页 | 9191在线播放 | 成人亚洲一区 | 97国产一区二区精品久久呦 | 成人高清视频在线观看 | 午夜精品久久久久久久久久久久 | 91在线视频播放 | 一区二区三区高清 |