Solved

Will StretchBlt work in .NET?

Posted on 2008-10-28
6
703 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 25

Expert Comment

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

Author Comment

by:LongDave
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 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