Draw border just inside non-transparent portion of image

浪尽此生 提交于 2019-12-06 14:08:00

问题


this thread is almost what I need: Creating a GraphicsPath from a semi-transparent bitmap

but he is drawing an outline AROUND the image - I need to draw one just a couple of pixels INSIDE the image. basically I'm trying to draw a "Focus Rectangle" (except it's not a rectangle) the result needs to be in the form of a GraphicsPath so i can fit it right into my current code.

private function GetImagePath(img as Image) as GraphicsPath ... end function

[EDIT] ok, first of all i cant even get the code from the other post to work. i keep getting index out of range at this line:

*byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3];*

that's IF it even gets past the other stuff first. alot of times it comes out of the first loop having not found a non-transparent point - when most of the image is non-transparent.

the newst problem is that it creates a list of point that dont make any sense. the image is 68x68 my list of points has points like 290x21 and even crazier ones like 2316x-15

this is my original image:

[wont let me upload cause im new]

its the background image of a button that is 70x70 - if that matters at all.


回答1:


I would think to use the routine provided in the link, get the output from it, and add your own wrapper around it modifying the path pushing it in the couple pixels you want. You could even make the amount of pixels to move in an input parameter so that you can play around with it.

Else you could add the second pass directly into the routine provided. Either way I think this is a 2 pass approach. You need to find the outer bounds of the object, that is what is provided. Then you need to essentially move around the path yourself, understand the derivative of the path so that you can move your path in perpendicularly to the derivative at the current point you are looking at. You will also have to recognize inside from outside (i.e. which direction to move the line).

An Algorithm might look something like this (remember just algorithm):

Create New List of Points for the new line

For all points i in 0 to (n-2) (points in original point list)
    // Get a New point in between
    float xNew = (x(i) + x(i + 1) ) / 2
    float yNew = (y(i) + y(i + 1) ) / 2

    // Need to figure out how much to move in X and in Y for each (pixel) move along
    // the perpendicular slope line, remember a^2 + b^2 = c^2, we have a and b
    float a = y(i + 1) - y(i)
    float b = x(i + 1) - x(i)
    float c = sqrt( (a * a) + (b * b) )

    // c being the hypotenus is always larger than a and b.
    // Lets Unitize the a and b elements
    a = a / c
    b = b / c

    // Assume the original point array is given in clockwise fashion
    a = -a
    b = -b

    // Even though a was calculated for diff of y, and b was calculated for diff of x
    // the new x is calculated using a and new y with b as this is what gives us the
    // perpendicular slope versus the slope of the given line
    xNew = xNew + a * amountPixelsToMoveLine
    yNew = yNew + b * amountPixelsToMoveLine

    // Integerize the point
    int xListAdd = (int)xNew
    int yListAdd = (int)yNew

    // Add the new point to the list
    AddPointToNewPointList(xListAdd, yListAdd)

Delete the old point list

Image of the geometry I am performing:



来源:https://stackoverflow.com/questions/10820802/draw-border-just-inside-non-transparent-portion-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!