Solved

Will StretchBlt work in .NET?

Posted on 2008-10-28
6
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 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