Re: VB6 versus VBNet Challenge



On Nov 29, 1:25 am, "Mike Williams" <mi...@xxxxxxxxxxxxxxxxx> wrote:
"Tom Shelton" <tom_shel...@xxxxxxxxxxx> wrote in message

news:e4a21924-00ea-4e34-83d4-4e252edc6594@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Mike...
I'm not sure if Michael C will ever respond - but, I would be
happy to have a go at a VB.NET version. The problem is
I'm not a graphics guy, by any stretch of the imagination, and
so I'm not sure what you mean by inverting the bitmap?

Well I've taken it to mean flipping each bit in each data byte in the image,
so that for example any bytes with &HFFFFFFFF (white) become &H00000000
(black) and any bytes with &H00000000 (black) become &HFFFFFFFF (white), and
of course with other data bytes being "bit flipped" regardless of the data
they contain. This causes the image to look a bit like a "funny coloured
negative" and of course if you flip the flipped image you end up with the
original image you started with. Running the function any odd number of
times will end up with a flipped image (hence his requirement of 1001 calls
to the function rasther than 1000, so you can see the result). Running it
1001 times again will give you back the image you started with.

And can you give me a quick
primer of the basic algorithm?

data = Not data

Mike

Ok...

VB.NET:

Option Strict On
Option Explicit On

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Diagnostics

Module Module1

Sub Main()
Dim bmp As New Bitmap("blizz2001-1024.bmp")
Dim sw As New Stopwatch()

sw.Start()
For i As Integer = 0 To 1000
ComplementImage(bmp)
Next i
Console.WriteLine(sw.Elapsed)

bmp.Save("blizz2001-1024 inverted.bmp")
End Sub

Private Sub ComplementImage(ByVal bmp As Bitmap)
Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0,
bmp.Width, bmp.Height), _
ImageLockMode.ReadWrite, _
bmp.PixelFormat)

Dim ptr As IntPtr = bmpData.Scan0
For i As Integer = 0 To bmpData.Height - 1
For j As Integer = 0 To bmpData.Width - 1
Dim component As Byte = Marshal.ReadByte(ptr)

Marshal.WriteByte(ptr, Not component)
ptr = New IntPtr(ptr.ToInt32() + 3)
Next
ptr = New IntPtr(ptr.ToInt32() + (bmpData.Stride -
bmpData.Width * 3))
Next

bmp.UnlockBits(bmpData)
End Sub
End Module

Release mode compile:
00:00:26.5968744

So, 26.6 seconds.

C#:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
static void Main ( string[] args )
{
Bitmap bmp = new Bitmap ( "blizz2001-1024.bmp" );
Stopwatch sw = new Stopwatch ();

sw.Start ();
for ( int i = 0; i < 1001; i++ )
{
CreateComplement ( bmp );
}
Console.WriteLine ( sw.Elapsed );
bmp.Save ( "blizz2001-1024 inverted.bmp" );
}

static void CreateComplement ( Bitmap bmp )
{
BitmapData bmpData = bmp.LockBits (
new Rectangle ( 0, 0, bmp.Width, bmp.Height ),
ImageLockMode.ReadWrite,
bmp.PixelFormat );

unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for ( int i = 0; i < bmpData.Height; i++ )
{
for ( int j = 0; j < bmpData.Width; j++ )
{
*ptr = (byte)( ~*ptr );
ptr += 3;
}
ptr += bmpData.Stride - bmpData.Width * 3;
}
}
bmp.UnlockBits ( bmpData );
}
}
}

Release mode compile:
00:00:00.7690147

Or about .8 seconds

My system - Intel Core 2 Duo E6600, 4GB Memory, Windows Vista Ultimate
32-bit.

--
Tom Shelton
.



Relevant Pages

  • Re: ICO to GIF? (transparant)
    ... i already converted to bmp and i used that code. ... transparent gif since i still use a bitmap object (GIF is bitmap so i should ... Dim xx As System.Drawing.Imaging.ImageFormat ... >> I'm trying to convert an icon handle to GIF. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: VB 2003: image/bitmap von bunt in Graustufen umwandeln
    ... Private Sub grey(ByVal bmp As Bitmap) ... Dim grau, blau, gruen, rot As Byte ... Dim bmpData As System.Drawing.Imaging.BitmapData ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Background image from embedded resource
    ... > I have a bitmap as an embedded resource. ... > background image of a button. ... > Dim bmp as bitmap = New Bitmap ...
    (microsoft.public.dotnet.general)
  • Background image from embedded resource
    ... I have a bitmap as an embedded resource. ... background image of a button. ... Dim bmp as bitmap = New Bitmap ...
    (microsoft.public.dotnet.general)
  • Re: VB3 is making my head spin!
    ... > and displaying it pixel by pixel] tell me how. ... compressed bitmap from an original 24 but full colour bitmap that is held on disk as a standard ... It shows you how to load a full colour 24bit .bmp file from disk and count ... Dim z As Long, t1 As Long, t2 As Long ...
    (comp.lang.basic.visual.misc)