Skip to content

Commit 2dc6661

Browse files
committed
Added "Trim Image" button to snippet settings
1 parent 562b8e3 commit 2dc6661

File tree

4 files changed

+285
-67
lines changed

4 files changed

+285
-67
lines changed

b2sbackglassdesigner/b2sbackglassdesigner/Modules/moduleB2S.vb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ Module moduleB2S
312312
Dim equal As Boolean = True
313313
If image1.Height = image2.Height AndAlso image1.Width = image2.Width Then
314314
Dim rect As Rectangle = New Rectangle(0, 0, image1.Width, image1.Height)
315-
Dim image1Data As System.Drawing.Imaging.BitmapData = DirectCast(image1, System.Drawing.Bitmap).LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, image1.PixelFormat)
316-
Dim image2Data As System.Drawing.Imaging.BitmapData = DirectCast(image2, System.Drawing.Bitmap).LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, image2.PixelFormat)
315+
Dim image1Data As System.Drawing.Imaging.BitmapData = DirectCast(image1, System.Drawing.Bitmap).LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image1.PixelFormat)
316+
Dim image2Data As System.Drawing.Imaging.BitmapData = DirectCast(image2, System.Drawing.Bitmap).LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image2.PixelFormat)
317317
Dim image1ByteCount As Integer = Math.Abs(image1Data.Stride) * image1.Height
318318
Dim image2ByteCount As Integer = Math.Abs(image2Data.Stride) * image2.Height
319319
If image1ByteCount = image2ByteCount Then
320320
Dim image1RGB(image1ByteCount) As Byte
321321
Dim image2RGB(image2ByteCount) As Byte
322322
System.Runtime.InteropServices.Marshal.Copy(image1Data.Scan0, image1RGB, 0, image1ByteCount)
323323
System.Runtime.InteropServices.Marshal.Copy(image2Data.Scan0, image2RGB, 0, image2ByteCount)
324-
For i As Integer = 0 To image1ByteCount
324+
For i As Integer = 0 To (image1ByteCount - 1)
325325
If Not image1RGB(i) = image2RGB(i) Then
326326
equal = False
327327
Exit For
@@ -335,6 +335,40 @@ Module moduleB2S
335335
Return equal
336336
End Function
337337

338+
Public Function TrimImage(ByRef _image As Image) As Rectangle
339+
Dim rect As Rectangle = New Rectangle(0, 0, _image.Width, _image.Height)
340+
Dim imageData As System.Drawing.Imaging.BitmapData = DirectCast(_image, System.Drawing.Bitmap).LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
341+
Dim imageByteCount As Integer = Math.Abs(imageData.Stride) * imageData.Height
342+
Dim imageARGB(imageByteCount) As Byte
343+
System.Runtime.InteropServices.Marshal.Copy(imageData.Scan0, imageARGB, 0, imageByteCount)
344+
DirectCast(_image, System.Drawing.Bitmap).UnlockBits(imageData)
345+
346+
Dim left As Integer = imageData.Width
347+
Dim right As Integer = 0
348+
Dim top As Integer = imageData.Height
349+
Dim bottom As Integer = 0
350+
For y As Integer = 0 To (imageData.Height - 1)
351+
For x As Integer = 0 To (imageData.Width - 1)
352+
Dim isNotTransparent As Boolean = imageARGB((((y * imageData.Width) + x) * 4) + 0) > 0
353+
If isNotTransparent Then
354+
If left > x Then
355+
left = x
356+
End If
357+
If right < x Then
358+
right = x
359+
End If
360+
If top > y Then
361+
top = y
362+
End If
363+
If bottom < y Then
364+
bottom = y
365+
End If
366+
End If
367+
Next
368+
Next
369+
Return New Rectangle(left, top, right - left + 1, bottom - top + 1)
370+
End Function
371+
338372
Public Function ImageToBase64(image As Image) As String
339373
If image IsNot Nothing Then
340374
With New System.Drawing.ImageConverter

b2sbackglassdesigner/b2sbackglassdesigner/forms/formSnippitSettings.Designer.vb

Lines changed: 16 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)