Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Project: NImage
- ' Autor: ernilos
- ' THIS FILE IS UNDER WTF LICENSE (http://www.wtfpl.net/)
- Public Class Form1
- Dim fName As String
- Private Sub OPEN_BIN_BTN_Click(sender As Object, e As EventArgs) Handles OPEN_BIN_BTN.Click
- Dim ofdlg As New OpenFileDialog
- If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
- fName = ofdlg.FileName
- Decrypt()
- End If
- End Sub
- Private Sub OPEN_BTN_Click(sender As Object, e As EventArgs) Handles OPEN_BTN.Click
- Dim ofdlg As New OpenFileDialog
- If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
- picture.Load(ofdlg.FileName)
- End If
- End Sub
- Private Sub SAVE_BIN_BTN_Click(sender As Object, e As EventArgs) Handles SAVE_BIN_BTN.Click
- Dim sfdlg As New SaveFileDialog
- If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
- fName = sfdlg.FileName
- Encrypt()
- End If
- End Sub
- Private Sub SAVE_BTN_Click(sender As Object, e As EventArgs) Handles SAVE_BTN.Click
- Dim sfdlg As New SaveFileDialog With {.Filter = "PNG File |*.png"}
- If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
- picture.Image.Save(sfdlg.FileName, Drawing.Imaging.ImageFormat.Png)
- End If
- End Sub
- Sub Decrypt()
- Try
- Dim bReader As New IO.BinaryReader(IO.File.Open(fName, IO.FileMode.Open))
- Dim W, H, block_W, base_X, X, Y As Int16
- W = bReader.ReadInt16
- block_W = W / 4
- H = bReader.ReadInt16
- If W = 0 And H = 0 Then
- Me.Text = "NImage [Failed - " & IO.Path.GetFileName(fName) & "]"
- Return
- End If
- X = 0
- Y = 0
- Dim bmp As New Bitmap(W, H)
- While bReader.BaseStream.Position <> bReader.BaseStream.Length
- Dim B = bReader.ReadByte
- Dim G = bReader.ReadByte
- Dim R = bReader.ReadByte
- Dim A = bReader.ReadByte
- bmp.SetPixel(X, Y, Color.FromArgb(A, R, G, B))
- X += 1
- If X = base_X + block_W Then
- X = base_X
- Y += 1
- End If
- If Y = H Then
- Y = 0
- base_X += block_W
- X = base_X
- End If
- End While
- picture.Image = bmp
- Me.Text = "NImage [" & IO.Path.GetFileName(fName) & "]"
- Catch ex As Exception
- Me.Text = "NImage [Failed - " & IO.Path.GetFileName(fName) & "]"
- End Try
- End Sub
- Sub Encrypt()
- Dim bWriter As New IO.BinaryWriter(IO.File.Open(fName, IO.FileMode.OpenOrCreate))
- Dim block_W, base_X, X, Y As Int16
- Dim done As Boolean = False
- Dim bmp As Bitmap = picture.Image
- bWriter.Write(CType(bmp.Width, Int16))
- block_W = bmp.Width / 4
- bWriter.Write(CType(bmp.Height, Int16))
- While Not done
- bWriter.Write(CType(bmp.GetPixel(X, Y).B, Byte))
- bWriter.Write(CType(bmp.GetPixel(X, Y).G, Byte))
- bWriter.Write(CType(bmp.GetPixel(X, Y).R, Byte))
- bWriter.Write(CType(bmp.GetPixel(X, Y).A, Byte))
- X += 1
- If X = base_X + block_W Then
- X = base_X
- Y += 1
- End If
- If Y = bmp.Height Then
- Y = 0
- base_X += block_W
- X = base_X
- End If
- If X = bmp.Width Then
- done = True
- End If
- End While
- bWriter.Flush()
- bWriter.Close()
- MsgBox("File Saved!", MsgBoxStyle.Information)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment