erni_hax

NImage SRC

Aug 28th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.79 KB | None | 0 0
  1. ' Project: NImage
  2. ' Autor:   ernilos
  3. ' THIS FILE IS UNDER WTF LICENSE (http://www.wtfpl.net/)
  4. Public Class Form1
  5.     Dim fName As String
  6.  
  7.     Private Sub OPEN_BIN_BTN_Click(sender As Object, e As EventArgs) Handles OPEN_BIN_BTN.Click
  8.         Dim ofdlg As New OpenFileDialog
  9.         If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  10.             fName = ofdlg.FileName
  11.             Decrypt()
  12.         End If
  13.     End Sub
  14.     Private Sub OPEN_BTN_Click(sender As Object, e As EventArgs) Handles OPEN_BTN.Click
  15.         Dim ofdlg As New OpenFileDialog
  16.         If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  17.             picture.Load(ofdlg.FileName)
  18.         End If
  19.     End Sub
  20.     Private Sub SAVE_BIN_BTN_Click(sender As Object, e As EventArgs) Handles SAVE_BIN_BTN.Click
  21.         Dim sfdlg As New SaveFileDialog
  22.         If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  23.             fName = sfdlg.FileName
  24.             Encrypt()
  25.         End If
  26.     End Sub
  27.     Private Sub SAVE_BTN_Click(sender As Object, e As EventArgs) Handles SAVE_BTN.Click
  28.         Dim sfdlg As New SaveFileDialog With {.Filter = "PNG File |*.png"}
  29.         If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  30.             picture.Image.Save(sfdlg.FileName, Drawing.Imaging.ImageFormat.Png)
  31.         End If
  32.     End Sub
  33.  
  34.     Sub Decrypt()
  35.         Try
  36.             Dim bReader As New IO.BinaryReader(IO.File.Open(fName, IO.FileMode.Open))
  37.             Dim W, H, block_W, base_X, X, Y As Int16
  38.             W = bReader.ReadInt16
  39.             block_W = W / 4
  40.             H = bReader.ReadInt16
  41.             If W = 0 And H = 0 Then
  42.                 Me.Text = "NImage [Failed - " & IO.Path.GetFileName(fName) & "]"
  43.                 Return
  44.             End If
  45.             X = 0
  46.             Y = 0
  47.             Dim bmp As New Bitmap(W, H)
  48.             While bReader.BaseStream.Position <> bReader.BaseStream.Length
  49.                 Dim B = bReader.ReadByte
  50.                 Dim G = bReader.ReadByte
  51.                 Dim R = bReader.ReadByte
  52.                 Dim A = bReader.ReadByte
  53.                 bmp.SetPixel(X, Y, Color.FromArgb(A, R, G, B))
  54.                 X += 1
  55.                 If X = base_X + block_W Then
  56.                     X = base_X
  57.                     Y += 1
  58.                 End If
  59.                 If Y = H Then
  60.                     Y = 0
  61.                     base_X += block_W
  62.                     X = base_X
  63.                 End If
  64.             End While
  65.             picture.Image = bmp
  66.             Me.Text = "NImage [" & IO.Path.GetFileName(fName) & "]"
  67.         Catch ex As Exception
  68.             Me.Text = "NImage [Failed - " & IO.Path.GetFileName(fName) & "]"
  69.         End Try
  70.     End Sub
  71.  
  72.     Sub Encrypt()
  73.         Dim bWriter As New IO.BinaryWriter(IO.File.Open(fName, IO.FileMode.OpenOrCreate))
  74.         Dim block_W, base_X, X, Y As Int16
  75.         Dim done As Boolean = False
  76.         Dim bmp As Bitmap = picture.Image
  77.         bWriter.Write(CType(bmp.Width, Int16))
  78.         block_W = bmp.Width / 4
  79.         bWriter.Write(CType(bmp.Height, Int16))
  80.         While Not done
  81.             bWriter.Write(CType(bmp.GetPixel(X, Y).B, Byte))
  82.             bWriter.Write(CType(bmp.GetPixel(X, Y).G, Byte))
  83.             bWriter.Write(CType(bmp.GetPixel(X, Y).R, Byte))
  84.             bWriter.Write(CType(bmp.GetPixel(X, Y).A, Byte))
  85.             X += 1
  86.             If X = base_X + block_W Then
  87.                 X = base_X
  88.                 Y += 1
  89.             End If
  90.             If Y = bmp.Height Then
  91.                 Y = 0
  92.                 base_X += block_W
  93.                 X = base_X
  94.             End If
  95.             If X = bmp.Width Then
  96.                 done = True
  97.             End If
  98.         End While
  99.         bWriter.Flush()
  100.         bWriter.Close()
  101.         MsgBox("File Saved!", MsgBoxStyle.Information)
  102.     End Sub
  103.  
  104. End Class
Advertisement
Add Comment
Please, Sign In to add comment