none
Best File Format to use in VB,net picturebox?

    Question

  • I have several AutoCAD drawings that I want to display in picture boxes in Visual Basic. I can generate almost any file format to use in the picture box but it seems that the image only shows very faintly no matter which format I use. Is there a trick to importing or using a file to make it show up correctly. I have tried .bmp, .png, .jpg and several other formats and none seem to work correctly. Does someone know if perhaps there is something related to AutoCAD that I need to do specifically for VB to be able to use the file I am generating?.

    JR

    Sunday, December 24, 2017 4:16 AM

Answers

  • I have several AutoCAD drawings that I want to display in picture boxes in Visual Basic. I can generate almost any file format to use in the picture box but it seems that the image only shows very faintly no matter which format I use.

    I know what you're talking about and there really isn't a great solution other than to go back to your drawing.

    A line (or any shape based on lines - which is all of them because AutoCAD isn't NURBS) is just one pixel wide. Obviously then, as an image it ends up showing up very faintly.

    Go back to your drawing and don't depend on pen settings: If you need to show depth then show the depth (for example, a wall's thickness as two lines with a hatch pattern in between).

    That will show up like you want - at that point, the choice of image type isn't all that important.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by J_Reid Monday, January 01, 2018 2:09 AM
    Sunday, December 24, 2017 11:53 AM
  • I am not sure if this is the only solution or even the best solution but it does work.

    Thanks


    JR

    • Marked as answer by J_Reid Monday, January 01, 2018 7:42 AM
    Monday, January 01, 2018 2:11 AM
  • I am very much aware of and familiar with the different file formats but thanks for the additional insight. Actually I have had this problem before and your suggestion to try a WMF is helpful. In the past I have had some luck using that particular file format although not always. I had not even thought about using PDF but that may be the best solution - I will try that as well. FYI - AutoCAD 2018 can directly generate several different file types including .WMF, .BMP and .PDF.  Thanks for the suggestions.

    JR


    Well Cor certianly has a way. I will try to get this in before the thread gets closed.  :)

    However it is not clear to me how you want to display the image from vb. And what you mean by "faintly".

    There is a difference between distorting a bitmap by resizing it when you draw it or show it in a picturebox and redrawing the orginal with a double line and fill in Autocad.

    Here is an example of drawing a metafile in a picturebox using vector scaling. As you can see there is no loss of relolution such as you see scaling a bitmap. The exact scaling of the lines widths can be a problem but its much better than a bitmap.

    If the bitmap is not resized then you should see the same image as you do in the original cad drawing image.

    So is this what you mean?

    If you can show or include a link to the result you get that you say is faint then we will have a better idea of what you mean.

    Imports System.Drawing.Imaging
    
    Public Class Form4
        Private m_metafile As New Metafile("c:\bitmaps\tipple1.wmf")
        Private ScaleRatio As Single = 1
        Private Rotation As Single = 0
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load
            BackColor = Color.Teal
            Padding = New Padding(10, 10, 10, 10)
            PictureBox1.Dock = DockStyle.Fill
        End Sub
    
        Private Sub Form4_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(Color.Black)
            DrawMetafile(m_metafile, e.Graphics, 0, 0, ClientRectangle.Width)
        End Sub
    
        Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
            PictureBox1.Focus()
        End Sub
    
        Private Sub PictureBox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseWheel
            ScaleRatio -= Math.Sign(e.Delta) * ScaleRatio / 12
            PictureBox1.Invalidate()
        End Sub
    
        Public Sub DrawMetafile(mf As Metafile, g As Graphics, x As Single, y As Single, w As Integer)
            'size proportional image to fit graphics
            Dim h As Integer = CInt(w * mf.Height / mf.Width)
            Dim dx As Single = CSng(x + (w / 2.0))
            Dim dy As Single = CSng(y + (h / 2.0))
    
            With g
                .TranslateTransform(dx, dy)                 'move 0,0 coordinate to the center of the image
                .ScaleTransform(ScaleRatio, ScaleRatio)     'resize
                .RotateTransform(Rotation)                  'rotate
                .TranslateTransform(-dx, -dy)               'move coordinates back
                .DrawImage(mf, x, y, w, h)                  'draw the new image 
            End With
        End Sub
    End Class




    • Edited by tommytwotrain Monday, January 01, 2018 4:21 AM
    • Marked as answer by J_Reid Monday, January 01, 2018 7:43 AM
    Monday, January 01, 2018 4:18 AM

All replies

  • Hi,

    Try using .ico or .dmg

    Or try the following formats

    • dwg
    • AutoCAD DXF
    • Design Web Format
    • ACIS
    • IGES
    • DGN
    • STL
    • Windows MetaFile
    • Shapefile
    • CAD Standards

    Thanks,


    Roshan - 7th Grade Student (Age 11) - Learning Small Basic, Visual Basic and C#

    Sunday, December 24, 2017 4:20 AM
  • The windows forms picturebox shows a bitmap. Whatever format you use. 

    Be aware that only you see currently what is "to work correctly". 

    Therefore maybe:  "You can try new glasses" could be a solution for that. 

    State your question better, what is "to work correctly"


    Success<br/> Cor

    Sunday, December 24, 2017 8:30 AM
  • As Cor says a picturebox only shows a bitmap format picture image. AutoCAD DWG format is a vector format.

    Do you understand the difference? A bitmap is basically an array of dot colors captured from the computer screen. If you resize the image then it will lose resolution. If you resize it several times it becomes a blurr. A bitmap is like a photograph.

    A vector file on the other hand can be redrawn at any size with the same screen resolution. A vector file is sort of like an array of distance and directions (vectors) and therefore can be re-drawn at any size.

    So the problem becomes when make a bitmap, AutoCAD is redrawing the vector file at the screen size in pixels and then if you show the bitmap on the screen it needs to be the same size as the original was drawn. If it is resized it will lose resolution.

    Therefore you need to make sure you save a very large bitmap in pixels (dots). How do you set the size when you save it from AutoCAD.

    So the bottom line is you cant easily do this with high accuracy. And how successful it is depends on your exact needs of what you want to see in the picturebox.

    You might be able to work with Windows Metafile Format which is a vector format. Does your AutoCAD save WMF or EMF format?

    Can you post an image of what you want to achieve?

    And then show the code you use to put the image in a picturebox in vb.

    PS another possibility is to save as PDF. I don't know if Acad does that now? If so that is more of a vector format that can be resized. However it requires 3rd party tools to show pdf in vb. I think Isharp is one.

    Sunday, December 24, 2017 9:51 AM
  • I have several AutoCAD drawings that I want to display in picture boxes in Visual Basic. I can generate almost any file format to use in the picture box but it seems that the image only shows very faintly no matter which format I use.

    I know what you're talking about and there really isn't a great solution other than to go back to your drawing.

    A line (or any shape based on lines - which is all of them because AutoCAD isn't NURBS) is just one pixel wide. Obviously then, as an image it ends up showing up very faintly.

    Go back to your drawing and don't depend on pen settings: If you need to show depth then show the depth (for example, a wall's thickness as two lines with a hatch pattern in between).

    That will show up like you want - at that point, the choice of image type isn't all that important.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by J_Reid Monday, January 01, 2018 2:09 AM
    Sunday, December 24, 2017 11:53 AM
  • I am not sure if this is the only solution or even the best solution but it does work.

    Thanks


    JR

    • Marked as answer by J_Reid Monday, January 01, 2018 7:42 AM
    Monday, January 01, 2018 2:11 AM
  • I am very much aware of and familiar with the different file formats but thanks for the additional insight. Actually I have had this problem before and your suggestion to try a WMF is helpful. In the past I have had some luck using that particular file format although not always. I had not even thought about using PDF but that may be the best solution - I will try that as well. FYI - AutoCAD 2018 can directly generate several different file types including .WMF, .BMP and .PDF.  Thanks for the suggestions.

    JR

    Monday, January 01, 2018 2:27 AM
  • Cor,
    I really appreciate your response and want to thank you for your critique of my question. Rarely have I seen such insightfulness, and unique ability to cut through all the extemporaneous B.S. and get right to the crux of the question. Not only are you able to see the problem but your ability to analyze it and then to discern a truly simple answer to such a complex problem is almost beyond comprehension. "Get a new pair of glasses" he says. I stand in awe.

    Two years ago I retired after almost 50 years of designing and fabricating various pressure containers for the Petro-Chemical industry. For probably the last 10 to 20 years of my career I spent an exorbitant amount of time each day answering questions for people who were probably smarter than me but lacked my experience and the knowledge that can only be obtained from numerous and repeated hands on experiences.

    There are a couple of reasons I am sharing this information with you:
    1. Based on the feedback I received, over many years, I believe that I communicate fairly effectively. This is further attested to by the fact that at least three other people that responded to my question seemed to understand what I was asking fairly well. On the other hand based on your writing I think I understand why you don't.
    2. One other thing I have learned, which I think is applicable here, it's never a good idea to respond to a question by questioning the question to cover up the fact that you don't have a clue.

    By the way, not being a bespectacled person your suggestion is rather flippant to say the least in addition to being totally useless, but thanks anyway. I have always been told that "if you are not part of the solution then you are part of the problem". I can only say that your response did not contribute anything to the solution.

    JR

    Monday, January 01, 2018 2:30 AM
  • I am very much aware of and familiar with the different file formats but thanks for the additional insight. Actually I have had this problem before and your suggestion to try a WMF is helpful. In the past I have had some luck using that particular file format although not always. I had not even thought about using PDF but that may be the best solution - I will try that as well. FYI - AutoCAD 2018 can directly generate several different file types including .WMF, .BMP and .PDF.  Thanks for the suggestions.

    JR


    Well Cor certianly has a way. I will try to get this in before the thread gets closed.  :)

    However it is not clear to me how you want to display the image from vb. And what you mean by "faintly".

    There is a difference between distorting a bitmap by resizing it when you draw it or show it in a picturebox and redrawing the orginal with a double line and fill in Autocad.

    Here is an example of drawing a metafile in a picturebox using vector scaling. As you can see there is no loss of relolution such as you see scaling a bitmap. The exact scaling of the lines widths can be a problem but its much better than a bitmap.

    If the bitmap is not resized then you should see the same image as you do in the original cad drawing image.

    So is this what you mean?

    If you can show or include a link to the result you get that you say is faint then we will have a better idea of what you mean.

    Imports System.Drawing.Imaging
    
    Public Class Form4
        Private m_metafile As New Metafile("c:\bitmaps\tipple1.wmf")
        Private ScaleRatio As Single = 1
        Private Rotation As Single = 0
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load
            BackColor = Color.Teal
            Padding = New Padding(10, 10, 10, 10)
            PictureBox1.Dock = DockStyle.Fill
        End Sub
    
        Private Sub Form4_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(Color.Black)
            DrawMetafile(m_metafile, e.Graphics, 0, 0, ClientRectangle.Width)
        End Sub
    
        Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
            PictureBox1.Focus()
        End Sub
    
        Private Sub PictureBox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseWheel
            ScaleRatio -= Math.Sign(e.Delta) * ScaleRatio / 12
            PictureBox1.Invalidate()
        End Sub
    
        Public Sub DrawMetafile(mf As Metafile, g As Graphics, x As Single, y As Single, w As Integer)
            'size proportional image to fit graphics
            Dim h As Integer = CInt(w * mf.Height / mf.Width)
            Dim dx As Single = CSng(x + (w / 2.0))
            Dim dy As Single = CSng(y + (h / 2.0))
    
            With g
                .TranslateTransform(dx, dy)                 'move 0,0 coordinate to the center of the image
                .ScaleTransform(ScaleRatio, ScaleRatio)     'resize
                .RotateTransform(Rotation)                  'rotate
                .TranslateTransform(-dx, -dy)               'move coordinates back
                .DrawImage(mf, x, y, w, h)                  'draw the new image 
            End With
        End Sub
    End Class




    • Edited by tommytwotrain Monday, January 01, 2018 4:21 AM
    • Marked as answer by J_Reid Monday, January 01, 2018 7:43 AM
    Monday, January 01, 2018 4:18 AM
  • Tommy,

    By faintly I meant that a black line on a white background is almost totally washed out. I don't have any examples left to show you but it no longer matters. What I am doing is using a picture box to display a graphic aid when the user moves the cursor into any one of several text boxes, similar to a tooltip. The file displayed in the picture box will show the user the specific data or dimension that is required for input.

    Your suggestion to use metafiles is exactly the correct solution. An additional benefit to using metafiles is that they recognize line weights. As I mentioned previously I have done this before but it was so long ago that I had forgotten how I did it.

    I had not planned to resize the pictureboxes but I like your code above and appreciate it as well. Thanks again for your input.


    JR

    Monday, January 01, 2018 8:35 AM
  • JR,

    Ok then. Your choice.

    However, I think we could spot the problem if we could reproduce it.

    What you describe does not make sense, to me, knowing what I know. Which is how to put images into a picturebox. And how to draw them. And how to use CAD. I author CAD software.

    If you capture a black on white line from AutoCad, and AutoCad saves the drawing image, as drawn on the screen, or redraws it to a bitmap, then if you show the bitmap in a picture box, any format bitmap, and show it at the original size it was captured/drawn at, then you should see the same thing as you saw in Autocad. If not then either original is messed up when Acad saved it, or it is not shown in the picturebox at its original size, etc.

    So when a problem like this comes up its sometimes helps others to solve it as well as the OP (original poster - you). And sometimes the forum members, me, would like to know.

    But its hard to do and its your question to do as you like.

    :)

    Monday, January 01, 2018 12:42 PM
  • Tommy,

    Sorry, I did not mean to leave anyone hanging with an unsolved problem. Actually you hit the nail on the head in your first response. The problem was the loss of resolution going from the AutoCAD Bitmap to the size of the picture box in the VB form. It turns out that the bitmap will work just fine so long as I scale the original up by 20 to 30 times in AutoCAD before I generate the bitmap file. The particular piece I was having problems with is a fairly small part. Some of the other parts are quite large. That also explains why some of them worked as is and the small ones had to be scaled up before generating the bitmap file.  

    However, your suggestion to use a metafile is a much better choice for my purposes because I am generating a number of files from different areas on the same drawing as well as other drawings and the metafiles work without any additional effort. Scaling each area or piece up, creating a bitmap file and then scaling back to the original size is a lot of extra trouble and prone to causing other problems.

    As for loading them into the picture box, I am simply setting the image of the picbox equal to whichever file is required. And in some cases changing the height or width of the box.

    I hope this is a better explanation of the problem I was having. I really do appreciate your response and your interest. Thanks. 


    JR

    Tuesday, January 02, 2018 4:04 AM
  • Tommy,

    Sorry, I did not mean to leave anyone hanging with an unsolved problem. Actually you hit the nail on the head in your first response. The problem was the loss of resolution going from the AutoCAD Bitmap to the size of the picture box in the VB form. It turns out that the bitmap will work just fine so long as I scale the original up by 20 to 30 times in AutoCAD before I generate the bitmap file. The particular piece I was having problems with is a fairly small part. Some of the other parts are quite large. That also explains why some of them worked as is and the small ones had to be scaled up before generating the bitmap file.  

    However, your suggestion to use a metafile is a much better choice for my purposes because I am generating a number of files from different areas on the same drawing as well as other drawings and the metafiles work without any additional effort. Scaling each area or piece up, creating a bitmap file and then scaling back to the original size is a lot of extra trouble and prone to causing other problems.

    As for loading them into the picture box, I am simply setting the image of the picbox equal to whichever file is required. And in some cases changing the height or width of the box.

    I hope this is a better explanation of the problem I was having. I really do appreciate your response and your interest. Thanks. 


    JR


    I see. Thanks for letting us know. I did not mean to guilt you we just tell what we know for the next question or others that may be following.

    Yeah. I see. Well you get the same kind of thing with a metafile but no where near as bad. A metafile captures the gdi vectors as they are drawn at the scale they are drawn on the graphics surface. So the line widths MAY be set by the size saved for the metafile which is the size of the graphics object it was drawn on. And the size in pixels of the line width when drawn. If your Acad line widths are allways one pixel then the size that is saved is always a 1 pixels line width. Then the metafile vector for the line is still an exact coordinates, the line widths are not, then are the widths that were drawn. If the line widths are scaled with the drawing, then you get the right width in the metafile.

    And so yes if you capture the whole drawing of say a transmission as a bitmap that is 1000 x 1000 pixels and the lines are 1 pixel wide. Then you go to vb with the bitmap and try to zoom into the key on the axle of one gear, a 100 x 100 area of the bitmap, and now show that in a picturebox that is 1000 x 1000 pixels on the screen in vb, and the picturebox is autosize, it scales the bitmap by 10 to fit the pitcurebox (ie 100 pixel image to 1000 pixel image). So now the line that was 1 pixel wide and black is 10 pixels wide and who knows what color, light gray as you also pickup the dithering with shades of gray from the original 1 pixel line as drawn in Acad.

    I think you Comprende now.

    So you need to save the bitmap from Acad as 10000 pixels wide then you can cut out a 1000 x 1000 pixel image of the keyway, show it in a 1000 x 1000 pixel picturebox and you will see what you saw in Acad. The problem is you don't know what the coordinates of the keyway are in the bitmap.

    Anyway, dont be a stranger.

    :)


    PS So see, I am describing what happens if you scale the bitmap that was drawn with 1 pixel line widths always. Frank is saying, draw the line with two lines a scaled distance apart, ie the width of a wall, then when you draw the bitmap on the screen at different sizes the wall width in pixels scales up and down with the rest of the vectors in the drawing. Now the wall width is a vector. Not just a 1 pixel dot in a bitmap. That's what Frank was describing I think.
    Tuesday, January 02, 2018 12:21 PM