473,672 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitmap blit speed (Graphics.DrawI mage)

Hi,

I've got a routine that fetches a bitmap from a COM server and converts it
into a .NET format bitmap. The original DIB surface is 24 bit. When
blitting this bitmap to a window, the speed is pretty good when the window
is around 1/4 the size of the screen, but if I go maximise the window, it
slows to a crawl (relative crawl). Anyway, I've read some blogs about how
to increase the speed, including using GDI instead of GDI+, DirectDraw or
DirectX but am not sure what the best way to proceed would be. I don't want
the weight of DirectX or DD just to render a bitmap to a window. Any ideas?

btw: I also read that using 32bpp instead of 24bpp would be considerably
faster with .NET GDI+, but am not sure how to do the conversion. The way I
get the bitmap from the DIB is shown below (also code gained from around the
net and modified for my own uses). I'm not sure if creating a new 32 bit
surface and blitting the 24 bit one to that will do here to convert it, i.e.
there must be a better way. Any ideas?

''' <summary>
''' Convert a DIB format bitmap into a .NET bitmap.
''' </summary>
''' <param name="theDIB"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ToBitmap(ByVal theDIB As IntPtr) As Bitmap

Dim theImage As Bitmap = Nothing
Dim theStream As MemoryStream = Nothing
Dim fh As New BITMAPFILEHEADE R
Dim bmiTyp As Type = GetType(BITMAPI NFOHEADER)
Try

' Check and "parse" the bitmap header.

Dim bmi As BITMAPINFOHEADE R =
DirectCast(Mars hal.PtrToStruct ure(theDIB, bmiTyp), BITMAPINFOHEADE R)

If bmi.biSizeImage = 0 Then
bmi.biSizeImage = ((((bmi.biWidth * bmi.biBitCount) + 31) And
(Not 31)) >3) * Math.Abs(bmi.bi Height)
End If

If ((bmi.biClrUsed = 0) And (bmi.biBitCount < 16)) Then
bmi.biClrUsed = 1 << bmi.biBitCount
End If

Dim fhSize As Integer = Marshal.SizeOf( GetType(BITMAPF ILEHEADER))
Dim dibSize As Integer = bmi.biSize + (bmi.biClrUsed * 4) +
bmi.biSizeImage
Dim chars() As Char = {"B"c, "M"c}

fh.Type = chars
fh.Size = fhSize + dibSize
fh.OffBits = fhSize + bmi.biSize + (bmi.biClrUsed * 4)
' serialise the bitmap into a memory stream.

Dim theData(fh.Size ) As Byte

If Not ToBytes(fh, theData) Then
Throw New Exception("Fail ed to serialize into structure.")
End If

Marshal.Copy(th eDIB, theData, fhSize, dibSize)
theStream = New MemoryStream(th eData)

theImage = New Bitmap(theStrea m)
Catch Ex As Exception

If Not theImage Is Nothing Then
theImage.Dispos e()
theImage = Nothing
End If

Finally

' Release objects.

If Not theStream Is Nothing Then
theStream.Close ()
theStream = Nothing
End If

End Try

Return theImage

End Function
''' <summary>
''' Serialize an object into a set of bytes.
''' </summary>
''' <param name="theObject "></param>
''' <param name="theData"> </param>
''' <returns></returns>
''' <remarks></remarks>
Private Shared Function ToBytes(ByVal theObject As Object, ByVal theData As
Byte()) As Boolean

Dim theHandle As GCHandle
Dim theBuffer As IntPtr

Try


Dim theSize As Integer = Marshal.SizeOf( theObject)

If (theSize theData.Length) Then
Throw New ArgumentExcepti on("Buffer too small ", " byte()
theData. ")
End If


theHandle = GCHandle.Alloc( theData, GCHandleType.Pi nned)
theBuffer = theHandle.AddrO fPinnedObject()
Marshal.Structu reToPtr(theObje ct, theBuffer, False)
Return True

Catch ex As Exception
Return False
Finally
' Release

If theHandle.IsAll ocated Then
theHandle.Free( )
End If
End Try

End Function

Apr 2 '07 #1
0 1591

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7350
by: news | last post by:
Hi Guys, Sorry if this is an obvious question but Im trying to rotate a bitmap and then save it. So far Ive only found how to rotates a Graphic but I can't find how to save it. I also want to crop the image and resize it, but I assume thats straight foward and for the code below Ive set the resize to 0.5 the original size and left the crop out. This is what Ive got so far :-
2
3020
by: Sharon | last post by:
I encountered a strange behavior when doing ‘new Bitmap’: The following code works fine and the given bitmap file is shown on the PictureBox (the m_DrawArea) in the correct bitmap sizes: private Graphics m_gMapImg; private Bitmap m_backroundImage; private Bitmap MapImg; private PictureBox m_DrawArea;
6
1807
by: Crirus | last post by:
Strange behaviour: I havea bitmap that I draw and add to an array of bitmaps... Do I have to create a bitmap using New every time? I tried to draw the bitmap in a for loop alike this: Dim mask As Image = getPicture("mask") Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height) Dim grMask As Graphics= Graphics.FromImage(tempMask) Dim x As Integer
2
4531
by: Peter Proost | last post by:
Hi group I have the following piece of code that should resize the bitmap in a picture box but it doesn't work as I tought it would. Can someone help me with it? thnx Peter Public Class Form1 Inherits System.Windows.Forms.Form
7
2016
by: Dennis | last post by:
I am trying to implement drawing on a bitmap and using bitblt to transfer it to the control graphics object in the paint event. It seems to draw on the bitmap ok but doesn't get transferred to the control graphics object in the paint event. Any help would be appreciated. Here is my code: public class as mycontrol Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nxDest As Integer, ByVal nyDest As...
4
13267
by: Andrew | last post by:
Hi, I'm trying to blit small bitmaps onto a larger bitmap, and I've got a few issues wrt positioning and output size. I think my problems are due to DPI differences... My small images are 72 DPI My Large image (which i need to create in code) needs to be 300 DPI.
5
2564
by: =?Utf-8?B?QVRU?= | last post by:
I have a bitmap of 100X100. On the load, the bitmap is created by a function (createimage()). On my OnPaint, I draw the image back to the screen (e.Graphics.DrawImage( bitmap, destrect)). Now, I want to add a button. When user click on it, I want the bitmap shift to the left by 10% and fill the new area with new information requested. Here is the code I have in the button handler: if (bitmap == null) return; Graphics graphics =...
7
8423
by: Stephen.Schoenberger | last post by:
Hello, I am reading in a bitmap image and storing it as a bitmap in C#. I need to perform some mathmatical operations on that image but it needs to be broken up into smaller fragments (16x16). On each of these fragments I need to perform my work, then write back the manipulated fragment to a new image. I have tried some different techniques but so far no luck. Any advice would be great! Thanks.
1
5593
by: martinsmith160 | last post by:
Hi all I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is working fine. My aim is to double click the picture box and the desired image will be drawn at the mouse position. This works fine unless I scroll or minimise the form because the image isnt repainted after movement. I looked up drawing the image to...
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.