Explanation of Processing Image to Byte Array - vb.net

Can someone explain me how an image converted to byte array?
I need the theory.
I want to use the image for AES encryption {VB .Net), so after I use OpenFile Dialog, my app will load the image and then process it into byte array, but I need the explanation for that process (how pixels turn into byte array)
Thanks for the answer and sorry for the beginner question.
Reference link accepted :)

When you read the bytes from the image file via File.ReadAllBytes(), their meaning depends on the image's file format.
The image file format (e.g. Bitmap, PNG, JPEG2000) defines how pixel values are converted to bytes, and conversely, how you get pixel values back from bytes.
The PNG and JPEG formats are compressed formats, so it would be difficult for you to write code to do that. For Bitmaps, it would be rather easy because it's a simple format. (See Wikipedia.)
But it's much simpler. You can just use .NET's Bitmap class to load any common image file into memory and then use Bitmap.GetPixel() to access pixels via their x,y coordinates.
Bitmap.GetPixel() is slow for larger images, though. To speed this up, you'll want to access the raw representation of the pixels directly in memory. No matter what kind of image you load with the Bitmap class, it always creates a Bitmap representation for it in memory. Its exact layout depends on Bitmap.PixelFormat. You can access it using a pattern like this. The work flow would be:
Copy memory bitmap to byte array using Bitmap.LockBits() and Marshal.Copy().
Extract R, G, B values from byte array using e.g. this formula in case of PixelFormat.RGB24:
// Access pixel at (x,y)
B = bytes[bitmapData.Scan0 + x * 3 + y * bitmapData.Stride + 0]
G = bytes[bitmapData.Scan0 + x * 3 + y * bitmapData.Stride + 1]
R = bytes[bitmapData.Scan0 + x * 3 + y * bitmapData.Stride + 2]
Or for PixelFormat.RGB32:
// Access pixel at (x,y)
B = bytes[bitmapData.Scan0 + x * 4 + y * bitmapData.Stride + 0]
G = bytes[bitmapData.Scan0 + x * 4 + y * bitmapData.Stride + 1]
R = bytes[bitmapData.Scan0 + x * 4 + y * bitmapData.Stride + 2]
A = bytes[bitmapData.Scan0 + x * 4 + y * bitmapData.Stride + 3]

Each Pixel is a byte and the image is made by 3 or 4 bytes, depending of its pattern. Some images has 3 bytes per pixel (related to Red, Greed and Blue), other formats may require 4 bytes (ALpha Channel, R, G and B).
You may use something like:
Dim NewByteArray as Byte() = File.ReadAllbytes("c:\folder\image")
The NewByteArray will be fulfilled with every byte of image and you need to process them using AES, regardless of its position or meaning.

Related

Rotation of a unit vector at a random point iby an angle along Y axis in 3D space

I had attached also the schematic to depict my question.
I need to rotate the vector V with the base point P by an angle and find the new vector V'.
The rotation axis is say for is about a local y axis at point P (which is parallel to global Y axis)
Subsequently, I need to rotate the initial vector V about x axis which is parallel to global Y axis.
The main reason for the rotation is to find the new vector V' at point P. Both the rotations are independent and each of the rotation provides a new V'. I'm programming this in VB.net and output is a double() of new vector V'.
Just apply the two rotations independently (see Wikipedia). The base point does not play any role in this because it is just a constant offset that never changes. If I got your description right, you want the following:
//rotation about y-axis
iAfterRot1 = cos(phi1) * i + sin(phi1) * k
jAfterRot1 = j
kAfterRot1 = -sin(phi1) * i + cos(phi) * k
//rotation about x-axis
iAfterRot2 = iAfterRot1
jAfterRot2 = cos(phi2) * jAfterRot1 - sin(phi2) * kAfterRot1
kAfterRot2 = sin(phi2) * jAfterRot1 + cos(phi2) * kAfterRot1

How can I write code that makes a spiral?

I just started learning to programming in Excel VBA, and I have a problem.
I want to draw a spiral with a program, but a can not solve it well.
I would like to use for and do until and do while cycles only.
Here is my code:
Sub spiral()
For i = 1 To 16
xmm = xmm - 1
ymm = ymm - 1
Do Until xp = i
xp = xp + 1
Cells(5 + xp, 5) = i
Loop
Do Until yp = i
yp = yp + 1
Cells(5 + xp, 5 + yp) = i + 1
Loop
Do Until xm > xp
xm = xm + 1
Cells(5 + xp + i * xmm, 5 + yp) = i + 2
Loop
Do Until ym > yp
ym = ym + 1
Cells(5 + xp + xmm, 5 + yp + i * ymm) = i + 3
Loop
Next i
End Sub
Thanks!
I can't think of any practical reason you'd need this, but that said, I'm often wasting time messing around with odd tasks "just to see if I can." 😊
Access VBA Circles
Recently a spiral was a self-assigned challenge I played with in SQL, although it originated with a circle in Access VBA from another useless project of mine:
Access World : Making an Analog Clock on an MS Access Form
This taught me a number of concepts including how to draw a circle on a grid.
Drawing a circle is almost the identical process as drawing the hands on that clock. If, rather than drawing the whole line of the clock hand, I instead draw only the tip of it (aka, the radius), all the way around, the 360°, then I have a circle.
SQL Circle
Say that ten times fast!
Then one day I wondered how hard it would be to convert the circle code to SQL Server, exploiting the graphing feature of the Stack Exchange Data Explorer (SEDE). A graph is drawn on a grid, so it's the same concept.
Here is one of several results (again, all useless):
SEDE : Draw Random Circles on chart
Click Run Query and then when it's finished calculating click the Graph tab. (You'll also need to either login with your Stack Exchange ID, or do the captcha.) There are various constants you can play with in the user prompts (below the SQL).
SQL Spiral
Then at some point I wondered about spirals.
(...but probably should have been time for me to get a life? Naaaa, lol)
A spiral is simply a modified circle except the radius decreases (or increases) as you progress around the circumference -- and you don't stop at 360° -- instead just keep going and going, and continually changing the radius at a steady rate.
SEDE : Draw a spiral
Again, click Run Query and then when it's finished calculating click the Graph tab. (You'll also need to either login with your Stack Exchange ID, or do the captcha.) There are various constants you can play with in the user prompts (below the SQL).
Spiral in Excel
I'm not going to waste any more time on spirals today, but if I was going to draw one in Excel worksheet cells, I would resize all cells on the worksheet to a tiny square grid (maybe width=1, height=10), and then I would borrow the code from the SQL example, adapting it to Excel (which woudn't take much work).
The Access VBA example may be more confusing to adapt since there a lot of programmatic manipulation of controls going on (which can't be done at runtime).
Or, the Access example could be duplicated in Excel using controls (ie., lines) instead of the grid method you were trying so far.
More Information:
Wikipedia : Midpoint Circle Algorithm
rosettacode : Midpoint circle algorithm examples (code in 39 languages)
tutsPlus.com : Working with circles/spirals
Math Overflow : How to spiral coordinates in an outward pattern on 2D grid?
Base on some answers from this thread (Looping in a spiral), I've managed to come with this solution
Sub spiral()
x = 0
y = 0
d = 1
m = 1
i = 1
j = 1
Do While i < 6
Do While 2 * x * d < m
Cells(x + 5, y + 5).Value = j
x = x + d
j = j + 1
Loop
Do While 2 * y * d < m
Cells(x + 5, y + 5).Value = j
y = y + d
j = j + 1
Loop
d = -1 * d
m = m + 1
i = i + 1
Loop
End Sub
Not sure if it is something that you are looking for thou, as you didn't specify the exact output that you want to achieve.

Understanding Quaternion equivalence to matrix transform

A quaternion is obviously equivalent to a rotation matrix, but a 4x4 matrix does more than just rotation. It also does translation and scaling.
A matrix for affine transforms can actually be represented with 12 elements because the last row is constant:
a b c d
e f g h
i j k l
0 0 0 1
x' = a*x + b*y + c*z + d
y' = e*x + f*y + g*z + h
z' = i*x + j*y + k*z + l
A full transform therefore takes 9 multiplies and 9 adds.
For the three affine transforms: rotation, scale, and translation I would like to know if a quaternion-based system is competitive. I have looked all over and have not found one anywhere.
Given a quaternion p = (w,x,y,z)
For rotation, q' = pqp'. I could add a translation vector: t=(tx,ty,tz)
q' = pqp' + t
That is just 7 elements as compared to 12 with matrices, though it is slightly more operations.
That still does not support scaling though. Is there a complete equivalent?
Note: If the only answer is to convert the rotation to a matrix, then that is not really an answer. The question is whether a quaternion system can perform affine transform without matrices.
If there is an equivalence, can anyone point me to a java or c++ class so I can see how this works?

How can I find (generate) data points form a shape in 2D in MATLAB ? For example, the letter A , B ,and C. Thanks

How can I find or generate data points form a shape in 2D in MATLAB ? For example, the letters A, B, and C.
You can use fill()
An example for an octogon, provided by
See https://www.mathworks.com/help/matlab/ref/fill.html
% Generate the points required for the fill.
t = (1/16:1/8:1)'*2*pi; % using 1/8 steps we get an 8 sided object.
x = cos(t);
y = sin(t);
% fill the data
fill(x,y,'r')
axis square % prevent skewing the result.
An example of generating the x y coordinates of a rectangle with an offset of (5,5):
x=[5 5 25 25 5]
y=[5 15 15 5 5]
You have 5 points because you need to include the final point to complete the path ( I believe ) Follow the blue path when collecting the x coordinates and the y coordinates. You can see we start at 5,5 then move to 5,15 --- so the first part of the path is
x=[5 5 ...
y=[5 15 ...
If you want to generate the coordinates automatically, you could use a program like InkScape (vector program) to help you convert a character to paths, but here is a simple example drawn with the pen tool:
The points are given by
m 0,1052.3622 5,-10 5,0 5,10 z
which 1052.3622 is VERY large, but is ultimately because I placed my shape at the bottom of the page. if we set this to be 0,0 it would go to the top of the page.

Convert 8 bytes into a Double in VB.net

I'm reading an ancient data file that is basically a flattened object store with type flags - for instance, 1=Int16, 2=Int32. To read the Int32's, for instance, I read 4 bytes out of the stream and then did this:
If B.Length >= 2 + Offset Then
Ans = Convert.ToUInt16(B(1 + Offset) * 256 + B(0 + Offset))
End If
Now I am at a bit of a loss how to do the 3=Double. These are 8-byte values, IEEE I assume. There is a Convert.ToDouble(byte), but that's not the same thing, that just returns a Double containing a value from 0 to 255. Likewise, Convert.ToDouble(Int64) basically just casts the value to Double.
So what's the trick here? I found threads for doing it in VB6 and C, but not VB.net.