copy exiting bmp to another bmp

Hi, i need to open a dialog box with only bmp files shown, identifie a existing file and copy it another,with out errors?
cheer paulvb

Comments

  • seancampbellseancampbell Pennsylvania, USA
    I dropped this code on a button click and it worked. I couldn't really understand what you were asking, I hope this does what you need.

    (This code prompts the use to select a Bitmap file and open it, it then prompts them to save that file and checks that the name changed. If there is an error openning the bitmap, the Catch section of the try will handle it. At that point you can send a message to the user to verify that he is openning an image file, or you can uncomment the Exception Message so you can see what error occured)

    [CODE]
    'Open File Dialog to select the file to copy
    Dim OFD As New OpenFileDialog
    OFD.Filter = "Bitmap | *.bmp"
    OFD.ShowDialog()

    Try
    'Load the file we want to copy into a Bitmap Object
    Dim BMP As New Bitmap(OFD.FileName)

    'Save File Dialog to select where to copy the file to
    Dim SFD As New SaveFileDialog
    SFD.Filter = "Bitmap | *.bmp"
    SFD.ShowDialog()

    'Check that the new file name is not the same as the old one
    'If you change folders, I beleive the filename will not be the same
    'Essentially keeping the program from overwriting the old one
    '(even though the new one is an exact copy)
    If SFD.FileName <> OFD.FileName Then
    BMP.Save(SFD.FileName)
    Else
    MsgBox("Please choose a new name for the file.")
    End If
    Catch Ex As Exception
    'For Testing Errors, uncomment below
    'MsgBox(Ex.Message)
    End Try
    [/Code]

    Sean Campbell
  • Hi Sean,

    Once again work well.

    Is there any way of not showing the save dialog box, just save to 1.bmp, without all ready exist box?

    Best Regards
    Paulvb
  • seancampbellseancampbell Pennsylvania, USA
    Sure, try this code:

    [code]

    'Open File Dialog to select the file to copy
    Dim OFD As New OpenFileDialog
    OFD.Filter = "Bitmap | *.bmp"
    OFD.ShowDialog()

    Try
    'Load the file we want to copy into a Bitmap Object
    Dim BMP As New Bitmap(OFD.FileName)

    'This sets Integer I to 1 and checks if it exists
    'It will increment the number by one until it finds
    'the first number that doesn't exist
    Dim I As Integer = 1
    Do While IO.File.Exists(CStr(I) & ".bmp")
    I += 1
    Loop

    'set the FileName to #.bmp (# is I)
    Dim FileName As String = CStr(I) & ".bmp"

    BMP.Save(FileName)

    Catch Ex As Exception
    'For Testing Errors, uncomment below
    'MsgBox(Ex.Message)
    End Try

    [/code]
  • Hi,

    I need to change to .pcx file, can you help.

    Best Reagrds
    Paulvb
  • seancampbellseancampbell Pennsylvania, USA
    Ok, Look at the code I posted. Everywhere it says Bitmap | *.bmp you need to change that to PCX | *.pcx ... pretty self explanitory.

    I don't wanna sound rude, but I suggest reading what the code is doing and understanding it, instead of just copy and pasting it. It would have been easy to make both changes you asked if you had just looked at it.

    With knowledge we grow, and one day it could be someone else asking these questions and you answering them (I joined this forum in 2003 or so as a newbie @ VB 6.0 now I am a full time .Net developer).

    Cheers,
    Hope the code works out for you
    Sean C
  • Hi Sean,

    Just a beginner!
    I have changed your code slightly overnight,

    Dim l2 As New OpenFileDialog()
    l2.InitialDirectory = "c:"
    l2.Filter = "Bitmap | *.bmp"
    If l2.ShowDialog() = DialogResult.OK Then
    TextBox8.Text = l2.FileName
    Dim bp2 As New Bitmap(l2.FileName)
    bp2.Save("c:2.bmp")
    End If

    Line 6 If i change Bitmap to PCX I have an error

    Thankyou
    Paulvb
  • seancampbellseancampbell Pennsylvania, USA
    Hmm, sorry dude, like I said I wasn't trying to be rude, just pointing out that understanding the code aids in recreating it in the future.

    Anyway, I mustve been really tired this morning to have a brain fart that big...

    Anyway, i was only thinking about changing the extension to PCX and not the image format itself. I took a look, and apparantly .Net doesn't have the functionality for encoding to PCX built in so you would have to download that encoder from somewhere, but it is able to export to the following formats:

    BMP
    EMF
    EXIF
    GIF
    ICON
    JPEG
    MEMORYBMP
    PNG
    TIFF
    WMF

    You can export to those formats like this:

    Dim BMP as New Bitmap
    BMP.Save("filename", System.Drawing.Imaging.ImageFormat.Tiff)

    Hope you get it worked out!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories