Solved

Will StretchBlt work in .NET?

Posted on 2008-10-28
6
721 Views
Last Modified: 2012-05-05
Hi, Experts -
I have an app that worked pretty well in VB6 that I'm importing to VB.net. Part of the VB6 code would take a custom font bitmap (6x8), write it into a picturebox, and then I would use stretchblt to bring it to the size that was needed in the display.

I've tried everything I can think of using the Graphics object in .net - it works, but the characters and images are all "fuzzy" as though "smoothing" is turned on. If I do a screen shot and zoom in, you can see where the 2-color (black font on khaki background) bitmap is no longer 2 colors and ONLY 2 colors - there are 'shades' around the lines.

I have tried turning smoothing to 'none' in the graphics objects, as well as playing with the pixel renderinghint, text contrast, and a bunch of other stuff. I cannot get the old "stretchblt" to work in .net. Can anyone suggest how to make the expanded images have SHARP definitions? MANY, MANY THANKS!
0
Comment
Question by:LongDave
  • 4
  • 2
6 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 22827401
Why doesn't StretchBlt work for you? Did you tried with this declaration?

Public Declare Function StretchBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal nXOriginDest As Integer, ByVal nYOriginDest As Integer, ByVal nWidthDest As Integer, ByVal nHeightDest As Integer, ByVal hdcSrc As IntPtr, ByVal nXOriginSrc As Integer, ByVal nYOriginSrc As Integer, ByVal nWidthSrc As Integer, ByVal nHeightSrc As Integer, ByVal dwRop As TernaryRasterOperations) As Boolean

For the dwRop parameter, you can use
For the IntPtr parameters, you can obtain it by using:
Dim pHDC As IntPtr = [graphics_object].GetHdc()
For example:
Dim pHDC As IntPtr = e.Graphics.GetHdc()

For the dwRop parameter, use this enum:

Public Enum TernaryRasterOperations As UInteger
    SRCCOPY = 13369376     'dest = source
    SRCPAINT = 15597702    'dest = source OR dest
    SRCAND = 8913094       'dest = source AND dest
    SRCINVERT = 6684742    'dest = source XOR dest
    SRCERASE = 4457256     'dest = source AND (NOT dest )
    NOTSRCCOPY = 3342344   'dest = (NOT source)
    NOTSRCERASE = 1114278  'dest = (NOT src) AND (NOT dest)
    MERGECOPY = 12583114   'dest = (source AND pattern)
    MERGEPAINT = 12255782  'dest = (NOT source) OR dest
    PATCOPY = 15728673     'dest = pattern
    PATPAINT = 16452105    'dest = DPSnoo
    PATINVERT = 5898313    'dest = pattern XOR dest
    DSTINVERT = 5570569    'dest = (NOT dest)
    BLACKNESS = 66     'dest = BLACK
    WHITENESS = 16711778   'dest = WHITE
End Enum

Try this and tell me if it worked.
0
 

Author Comment

by:LongDave
ID: 22828101
Sorry for the delay - had to go to my son's hockey game ... yes, this is the same declaration I used. The return value is non-zero, which indicates it should be working-but nothing appears on the picturebox image.

I'm making a graphics object of the image of a picturebox, then passing the IntPtr from it to a different class. I'm expecting the hdc (this is the desthdc)  to be static, and in use continuously. (Is this wrong?) The second hdc (srchdc) is generated from a listimage.images(x) object.

I really wish I knew why the graphics object DrawImage method is doing this to me!  I'll revisit the stretchblt, but do you have any other ideas? Like I say, it actually works with the graphics objects, it just doesn't look "crisp"  like the original app did.

Thanks!

0
 

Author Comment

by:LongDave
ID: 22830771
I've attached the main part of the code that is giving me problems. The sub "SetUpDisplayBuffer" creates an in-memory bitmap called "ClockDisplayBuffer", and an associated graphics object CDFGraphic. I know this routine works, because if I transfer the ClockDisplayBuffer bitmap to a PictureBox in StretchImage mode, the picture comes through fine (except a bit fuzzy - the source of this whole question)

WriteOnly Property AccessBuffer passes a PictureBox from the main form. A Graphics object is created from the PictureBox.Image. and I then attempt to use StretchBlt to copy the image from CDFGraphic to g. But nothing shows up in the PictureBox. Any hints would be greatly appreciated.

    Public Sub SetUpDisplayBuffer(ByVal tWidth As Integer, ByVal tHeight As Integer, ByVal tformat As Image)
        BufferWidth = tWidth
        BufferHeight = tHeight
 
        Dim g As Graphics = Graphics.FromImage(tFormat)
        ClockDisplayBuffer = New Bitmap(21 * CellWidth, 8 * CellHeight, g)
        CDFGraphic = Graphics.FromImage(ClockDisplayBuffer)
        CDFGraphic.SmoothingMode = SmoothingMode.None
        OutHeight = CDFGraphic.VisibleClipBounds.Height
        OutWidth = CDFGraphic.VisibleClipBounds.Width
    End Sub
 
    WriteOnly Property AccessBuffer() As System.Windows.Forms.PictureBox
        Set(ByVal value As System.Windows.Forms.PictureBox)
 
            Dim g As Graphics = Graphics.FromImage(value.Image)
            Dim Thishdc As IntPtr = g.GetHdc
            Dim Thathdc As IntPtr = PS.CDFGraphic.GetHdc
            Dim ans As Integer = StretchBlt(Thishdc, 1, 1, value.Width, value.Height, Thathdc, 0, 0, PS.OutWidth, PS.OutHeight, 13369376)
            PS.CDFGraphic.ReleaseHdc()
            g.ReleaseHdc()
            ScreenIsReady = False
        End Set
    End Property

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 25

Expert Comment

by:Luis Pérez
ID: 22839221
If you could post the complete code, the maybe I can help you.
0
 

Author Comment

by:LongDave
ID: 22840083
The complete code is 4450 lines, not including about 44k of resources - it might be a bit cumbersome to post it.
0
 

Accepted Solution

by:
LongDave earned 0 total points
ID: 22936602
Although I didn't get Stretchblt to work, I did create a workaround. Basically I had to re-create the fonts an a pixel-by-pixel basis, multiplying the x and y coordinates based on the scale factor. By creating them in this fashion, rather than using stretchblt, everything worked out.

Not the solution I would have liked, however! :(
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question