VB.NET using bitblt to draw bitmap renders a black square - vb.net

I'm just starting out with VB.net after many years of VB 6.0 and I thought I'd get started with some graphics.
Just for fun I'm using BitBlt to draw a bitmap I have loaded as a resource and draw it on a form. I can load an existing image from a picturebox and bitblt it, which works fine. I can also load the bitmap from resources and store it into the picturebox, but when I take the bitmap, turn it into a GDI Graphics object and BitBlt it onto the form, all I get is a black square that is the same size as the bitmap.
Here is my code:
Dim srcBmp As New Bitmap(Me.GetType, "colorwheel.bmp")
Dim drect As New Rectangle(0, 0, 233, 233)
'srcGrp = PictureBox1.CreateGraphics 'This works.
srcGrp = Graphics.FromImage(srcBmp) 'This doesn't
targetGrp = Me.CreateGraphics 'destination graphics
srcHdc = srcGrp.GetHdc
TargetHdc = targetGrp.GetHdc
BitBlt(TargetHdc, 0, 0, 233, 233, srcHdc, 0, 0, SRCCOPY)
srcGrp.ReleaseHdc(srcHdc)
targetGrp.ReleaseHdc(TargetHdc)
targetGrp.Dispose()
srcBmp.Dispose()
srcGrp.Dispose()
The reasons I want to use bitblt are a) I've used it before, so it was my first choice. b) I know that it is possible to use a black and white mask to mask out some areas as transparent, which is what I need. and b) the graphics may be moving around frequently.
Is there a better way of drawing graphics with a transparent background onto a form or picturebox? e.g. a png with an alpha channel?

worked it out.
1) load bitmap
2) get Hbitmap from bitmap
3) create blank DC stream
4) create compatible DC from DC stream
Dim srcBmp As New Bitmap(Me.GetType, "colorwheel.png")
Dim hbm As IntPtr = srcBmp.GetHbitmap()
Dim sdc As IntPtr = GetDC(IntPtr.Zero)
Dim hdc As IntPtr = CreateCompatibleDC(sdc)
you can then bitblt with the hdc. Found that bitblt doesn't work in .net the same as it used to in vb6. I'm going to try the DrawImage method instead.

Related

How do I make an image not selectable

I have added an image to my iTextSharp PDF document like this:
Public Sub CreatePDFFromBitmap(ByVal uPath As String, ByVal uBitmap As Bitmap)
Dim nFs As System.IO.FileStream = New FileStream(uPath, FileMode.Create)
Dim nDocument As iTextSharp.text.Document
Dim nWriter As iTextSharp.text.pdf.PdfWriter
Dim nCb As iTextSharp.text.pdf.PdfContentByte
Dim nImgFromBitmap As System.Drawing.Image = DirectCast(uBitmap, System.Drawing.Image)
Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(nImgFromBitmap, Imaging.ImageFormat.Png)
Dim bLandscape As Boolean = (nImg.Width > nImg.Height)
'rotation needs to be set before document is being opened
If bLandscape Then
nDocument = New iTextSharp.text.Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Else
nDocument = New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
End If
'if an exception is raised here, the following will help: https://stackoverflow.com/questions/15833285/pdfwriter-getinstance-throws-system-nullreferenceexception
nWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(nDocument, nFs)
nDocument.Open()
nCb = nWriter.DirectContent
nImg.ScaleToFit(nDocument.PageSize.Width, nDocument.PageSize.Height) 'raises dpi size :-)))
'X-Y-Koordinatensystem 0,0 startet also unten links, nicht oben-links
nImg.SetAbsolutePosition(0, nDocument.PageSize.Height - nImg.ScaledHeight)
nCb.AddImage(nImg)
nDocument.Close()
nWriter.Close()
nFs.Close()
End Sub
It works fine.
However, when I click the image in the PDF, it gets selected.
This is not what I want.
If I click the image in the PDF, it should not be selected.
This is what it looks like: The image becomes blue:
I want to add editable fields to the PDF, so I need to make the image not selectable, else it would confuse the user.
As Abdel-Rahman Al-Qawasmi mentions in his answer, it is completely up to the PDF viewer which entities it makes selectable and which not. Thus, there is no guaranteed way to get what you want.
Nonetheless, there are ways to put an image into a PDF which dissuade current versions of most PDF viewers from making it selectable. These ways either transform the bitmap image into a non-bitmap entity (e.g. by iterating over the pixels of the bitmap and drawing a little rectangle per pixel using vector graphics) or wrap the bitmap image into something that usually is not selectable.
Let's take the latter approach and wrap the image into a page-size PDF pattern with which we then fill the actual page. You can do that by replacing your
nCb.AddImage(nImg)
by
Dim painter As iTextSharp.text.pdf.PdfPatternPainter = nCb.CreatePattern(nDocument.PageSize.Width, nDocument.PageSize.Height)
painter.AddImage(nImg)
nCb.SetColorFill(New iTextSharp.text.pdf.PatternColor(painter))
nCb.Rectangle(0, 0, nDocument.PageSize.Width, nDocument.PageSize.Height)
nCb.Fill()
(This essentially is the VB/iTextSharp pendant of the Java/iText code from this answer.)
This is a pdf program specifications and not related to asp.net or vb.net programming. you need to have control of the pdf reader settings. Or try to use another format.

Is it possible to view multi page .Tif files in vb.net application?

I am hoping to be able to view .Tif files in my vb.net application - is there a component that can be used to do that. I've been messing around with Tiff Viewer but for some reason the file is stretched beyond anything - you can barely make out what it says. I tried to adjust the width of the viewer but it did not do much, it is still stretched horizontally.
Based on https://stackoverflow.com/a/401579/741136, this will load a multiframe tif into a collection of single frames:
Function LoadTif(filename As String) As List(Of Image)
Dim lstTif As New List(Of Image)
Dim bmp As Bitmap = DirectCast(Image.FromFile(filename), Bitmap)
For i As Integer = 0 To bmp.GetFrameCount(Imaging.FrameDimension.Page) - 1
bmp.SelectActiveFrame(Imaging.FrameDimension.Page, i)
Dim ms As New System.IO.MemoryStream
bmp.Save(ms, Imaging.ImageFormat.Tiff)
lstTif.Add(Image.FromStream(ms))
ms.Dispose()
Next i
Return lstTif
End Function

How to capture Mouse Pointer through VB .Net?

I have written code in vb .net to take a screenshot of the screen. It captures the screen, but it doesn't capture mouse pointer. My code is below
Private Sub Take_Clip()
Dim graph As Graphics
Dim bnd As Rectangle = My.Computer.Screen.Bounds
Dim SS As Bitmap
SS = New Bitmap(bnd.Width, bnd.Height, Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(SS)
graph.CopyFromScreen(0, 0, 0, 0, bnd.Size)
End Sub
How to modify this code to capture mouse pointer also?

Capture image of webbrowser control

I want to capture the image of my webbrowser, or actually just a part of it.
I already achieved it, but it just takes a screenshot of it instead of capturing the bitmap itself.
So when i use this technique and you drag another window above it, the window is also captured. (this is what I don't want.)
My code:
' 1. Get the WebBrowsers bitmap.
Dim bmp As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
Dim BMPEndResult As Bitmap
Dim graph As Graphics = Graphics.FromImage(bmp)
Dim p As Point = Me.PointToScreen(WebBrowser1.Location)
graph.CopyFromScreen(p.X, p.Y, 0, 0, bmp.Size)
picDest.Image = bmp
So I need to get the image of the webbrowser:
- without the scrollbars if possible
- Even if the window is minimized or in the task bar
- Full webbrowser
- and also if possible just a part of it, where I can specify the top, left, width and height
WebBrowser.DrawToBitmap(bitmap, rectangle)

slicing a jpg with vb.net

since i do not have any image editing software, i am going to use vb.net to slice an image horizontally. can someone help me get started please?
In the code below you first load the image, then create a new image with the new width and height, grab the Graphics object from it and finally draw the old image onto the new image. We draw the old image onto the new image using the old image's dimensions but since the new image is smaller the rest will be off of the canvas.
Private Shared Sub CropImage(ByVal inputImagePath As String, ByVal outputImagePath As String, ByVal newHeight As Integer)
Using oldImage = System.Drawing.Image.FromFile(inputImagePath)
Using NewImage As New System.Drawing.Bitmap(oldImage.Width, newHeight)
Using G = Graphics.FromImage(NewImage)
G.DrawImage(oldImage, 0, 0, oldImage.Width, oldImage.Height)
NewImage.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
End Using
End Using
End Sub