久久久久久久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(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 二区中文字幕 | 国产97久久 | 亚洲日本乱码在线观看 | 亚洲精品视频免费观看 | 91免费高清 | 成人精品一区二区三区 | 久久国产精品视频观看 | 成人免费视频观看视频 | 在线观看av网站永久 | 男人天堂网址 | 国产成人综合在线 | 国产精品久久精品 | 精品欧美一区二区在线观看视频 | 亚洲国产成人精品久久 | 欧美女优在线观看 | 黄色片视频免费 | 欧美精品中文字幕久久二区 | 日本黄色免费片 | 国产1区| 国产精品1区2区3区 欧美 中文字幕 | 一区二区三区在线电影 | 精品一区二区三区91 | 另类视频区 | 久久精品小视频 | 亚洲精品免费在线 | 日韩av啪啪网站大全免费观看 | 午夜免费网站 | 国产超碰人人爽人人做人人爱 | 99pao成人国产永久免费视频 | 国产在线一区二区三区 | 四虎成人免费视频 | 国产精品一区二区三区久久久 | 免费在线观看av网站 | 91av在线视频观看 | a国产视频 | 国产精品1区2区 | 免费看一级毛片 | 久久99精品久久久 | 超碰av在线 | 国产精品毛片无码 | 黄色网一级片 |