首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在DrawEllipse数组中使用LockBits (如何生成一组形成椭圆的像素)

如何在DrawEllipse数组中使用LockBits (如何生成一组形成椭圆的像素)
EN

Stack Overflow用户
提问于 2015-07-20 16:53:26
回答 1查看 195关注 0票数 1

我使用这个类根据LockBits函数填充位图的像素:

代码语言:javascript
复制
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices.Marshal

Public Class Fill

    Public Shared Function Process(ByVal b As Bitmap) As Bitmap

    Dim bmd As BitmapData = _
    b.LockBits(New Rectangle(0, 0, b.Width, b.Height), _
    System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)

    Dim scan0 As IntPtr = bmd.Scan0
    Dim stride As Integer = bmd.Stride

    ' Here's the speedier method.
    ' define an array to store each pixels color as an int32 
    Dim pixels(b.Width * b.Height - 1) As Integer

    ' this is system.runtime.interopservices.marshall.copy
    Copy(scan0, pixels, 0, pixels.Length)

    ' loop through all pixels and fill

    For i As Integer = 0 To pixels.Length - 1
        pixels(i) = Color.Red.ToArgb
    Next

    ' Copy the data back from the array to the locked memory
    Copy(pixels, 0, scan0, pixels.Length)

    ' finally we unlock the bits. 
    b.UnlockBits(bmd)
    Return b
    End Function
End Class

现在,我不需要填充所有的像素,我需要填充一个椭圆(实际上它会是很多椭圆,这就是我使用LockBits的原因),所以我搜索了一种使用某种公式逐个像素绘制椭圆像素的方法,但是我没有找到多少帮助,而且我对这个数学问题也不太擅长。那么,我的问题是:如何创建一个像素数组,形成一个填充的椭圆?谢谢

补充(请随意忽略):

我会解释清楚我想做什么,这样也许能帮助你理解我的处境。实际上,我正在处理一个函数,它应该在位图的特定区域生成任意宽度和高度(在特定范围内)的填充椭圆,而填充的像素必须有该区域像素总数的百分比,这就是为什么我需要逐像素绘制椭圆像素(或使用像素数组)来跟踪填充像素的数量。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-20 18:32:39

椭圆内所有点的公式是:

(x - h) * (x - h) / a * a + (y - k) * (y - k) / b * b <= 1

哪里

x,y are the coordinates of any point on the ellipse

a, b are the radius on the x and y axes respectively

h,k the coordinates of the center

所以密码:

代码语言:javascript
复制
Dim h, k, a, b, x, y As Integer
Dim res As Double

'arbitrary values of ellipse
h = 200
k = 200
a = 80
b = 60

For x = h - a To h + a
    For y = k - b To k + b
        res = CDbl((x - h) * (x - h)) / CDbl(a * a) + CDbl((y - k) * (y - k)) / CDbl(b * b)

        If res <= 1.0 Then
            'the point (x, y) is inside
        End If

    Next
Next
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31522063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档