如何创建比特的数组,并把它们变成一个位图VB? [英] How to create an array of bits and turn them into a bitmap VB?

查看:158
本文介绍了如何创建比特的数组,并把它们变成一个位图VB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个位数组,并把它们变成VB中的一个位图?我知道如何创建这样一个位图昏暗NBitmap新位图(宽,高),然后编辑它的像素是这样 NBitmap.setpixel(X ,Y,颜色)。这是缓慢的,但是我认为,创建阵列或东西然后将其转换为位图会更快。但我不知道任何帮助会怎样......该多好!
例如:

I want to create an array of bits and turn them into a bitmap in vb? I know how to create a bitmap like this Dim NBitmap as new bitmap(width,height), Then edit its pixels like this NBitmap.setpixel(x,y,color). This is slow however I thought that creating an array or something then converting it to a bitmap would be faster. But i don't know how... Any help would be nice! Example:

Dim SM = DateTime.Now.Millisecond
Dim Texture As Bitmap = Image.FromFile("C:\Users\Noah\Desktop\graphics\Grass.jpg")
Dim w = 600
Dim h = 600
Dim Pixels(600, 600) As Color
Dim x = 0
Do Until x = 600
    Dim y = 0
    Do Until y = 600
        Pixels(x, y) = Texture.GetPixel(x, y)
        y += 1
    Loop
    x += 1
Loop
Dim EM = DateTime.Now.Millisecond
Dim FM = EM - SM
If FM < 0 Then
    FM += 1000
End If
Dim FPS = 1000 / FM
Dim ETDFrame As New Bitmap(600, 600)
ETDFrame.image = Pixels().convertBMP

我知道,这code这么想的工作,但如果给了一个例子,如果我want.My的目标是创建一个新的位图和编辑所有的像素超级快,快够一个3D游戏...

I know that this code dosen't work but if gives an example if what i want.My goal is to create a new bitmap and edit all the pixels super fast, fast enough for a 3d game...

推荐答案

目前还不清楚是什么你正在尝试做的。看来,如果你想为.jpg转换为.bmp。如果是这样,你可以调用 .Save Image类的方法:

It's not clear what you are trying to do. It appears as if you are trying to convert a .jpg to a .bmp. If that is so, you can just call the .Save method of the Image class:

'Load the bitmap
Dim bm As Bitmap = Image.FromFile("C:\Users\Noah\Desktop\graphics\Grass.jpg")
'Save as .bmp
bm.Save("C:\Users\Noah\Desktop\graphics\Grass.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

如果你真的需要用像素来上班,然后用 GetPixel 的setPixel 将是太慢了。您需要使用 LockBits 直接与位图数据的工作。事情是这样的:

If you really need to work with the pixels, then using GetPixel and SetPixel will be too slow. You need to use LockBits to work directly with the bitmap data. Something like this:

Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Private Sub DoGraphics()
    Dim x As Integer
    Dim y As Integer

    'PixelSize is 3 bytes for a 24bpp Argb image.
    'Change this value appropriately
    Dim PixelSize As Integer = 3

    'Load the bitmap
    Dim bm As Bitmap = Image.FromFile("C:\Users\Noah\Desktop\graphics\Grass.jpg")

    'lock the entire bitmap for editing
    'You can change the rectangle to specify different parts of the image if needed.
    Dim bmData As BitmapData = bm.LockBits(New Rectangle(0, 0, bm.Width, bm.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat)

    'Declare empty Color array
    Dim pixels(bm.Width - 1, bm.Height - 1) As Color

    'loop through the locked area of the bitmap.
    For x = 0 To bmData.Width - 1
        For y = 0 To bmData.Height - 1
            'Get the various color offset locations for each pixel.
            'This calculation is for a 24bpp rgb bitmap
            Dim blueOfs As Integer = (bmData.Stride * x) + (PixelSize * y)
            Dim greenOfs As Integer = blueOfs + 1
            Dim redOfs As Integer = greenOfs + 1

            'Read the value for each color component for each pixel
            Dim red As Integer = Marshal.ReadByte(bmData.Scan0, redOfs)
            Dim green As Integer = Marshal.ReadByte(bmData.Scan0, greenOfs)
            Dim blue As Integer = Marshal.ReadByte(bmData.Scan0, blueOfs)

            'Create a Color structure from each color component of the pixel
            'and store it in the array
            pixels(x, y) = Color.FromArgb(red, green, blue)
        Next
    Next

    'Do something to the pixels array here:
    For x = 0 To bmData.Width - 1
        For y = 0 To bmData.Height - 1
            pixels(x, y) = Color.Red
        Next
    Next

    'Update the bitmap from the pixels array
    For x = 0 To bmData.Width - 1
        For y = 0 To bmData.Height - 1
            'Get the various color offset locations for each pixel.
            'This calculation is for a 24bpp rgb bitmap
            Dim blueOfs As Integer = (bmData.Stride * x) + (PixelSize * y)
            Dim greenOfs As Integer = blueOfs + 1
            Dim redOfs As Integer = greenOfs + 1

            'Set each component of the pixel
            'There are 3 bytes that make up each pixel (24bpp rgb)
            Marshal.WriteByte(bmData.Scan0, blueOfs, pixels(x, y).B)
            Marshal.WriteByte(bmData.Scan0, greenOfs, pixels(x,y).G)
            Marshal.WriteByte(bmData.Scan0, redOfs, pixels(x,y).R)

        Next
    Next

    'Important!
    bm.UnlockBits(bmData)

    'Save the updated bitmap
    bm.Save("C:\Users\Noah\Desktop\graphics\Grass.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

End Sub

我希望这有助于。

I hope this helps.

更新:我已经更新了code显示变幻的像素值

UPDATE: I have updated the code to show changing the pixel values.

这篇关于如何创建比特的数组,并把它们变成一个位图VB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆