Solved

Will StretchBlt work in .NET?

Posted on 2008-10-28
6
707 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now