Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello once again !

could someone tell me how could i save an image that i drew (painted) on it with the same image quality as the original image ?

i'm using this code to save the image :

VB
Using Bmp As New Bitmap(PictureBox4.Image, defaultWidth, defaultHeight)

            Using G As Graphics = Graphics.FromImage(Bmp)

                G.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
                G.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half

                G.DrawImage(Bmp, 0, 0, defaultWidth, defaultHeight)

                Bmp.Save(str1 & "\" & str2, Imaging.ImageFormat.Jpeg)
                Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
                Dim myBitmap As New Bitmap(Drawbitmap)
                Dim myThumbnail As Image = myBitmap.GetThumbnailImage(100, 60, myCallback, IntPtr.Zero)
                myThumbnail.Save(str1.Replace("Saved", "Thumbnails") & "\" & str2)
                Bmp.Dispose()

            End Using
        End Using


the thing is the original image has 1920x1080 ,when i load the image into a picturebox i use the bellow code to scale the bitmap to fit the picturebox :

VB
Dim PicBoxHeight As Integer
        Dim PicBoxWidth As Integer
        Dim ImageHeight As Integer
        Dim ImageWidth As Integer
        Dim TempImage As Image
        Dim scale_factor As Single
        PictureBox4.SizeMode = PictureBoxSizeMode.Normal
        ' Get control size
        PicBoxHeight = PictureBox4.Height
        PicBoxWidth = PictureBox4.Width
        ' Load the image. Change Image.FromFile() to match your code
        TempImage = Image.FromFile(str)
        defaultWidth = TempImage.Width
        defaultHeight = TempImage.Height
        ' Get image size
        ImageHeight = TempImage.Height
        ImageWidth = TempImage.Width
        ' Calculate scale_factor
        scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
        ' 1) Height
        If ImageHeight > PicBoxHeight Then
            ' Reduce height first
            scale_factor = CSng(PicBoxHeight / ImageHeight)
        End If
        ' 2) Width. Notice, we do know now how much we have to scale down the height
        ' and the scale_factor affects width too
        If (ImageWidth * scale_factor) > PicBoxWidth Then
            ' Scaled width exceeds Box's width, recalculate scale_factor
            scale_factor = CSng(PicBoxWidth / ImageWidth)
        End If
        ' Move image to control for resizing
        PictureBox4.Image = TempImage
        ' Get the source bitmap.
        Dim bm_source As New Bitmap(PictureBox4.Image)
        ' Make a bitmap for the result.
        Dim bm_dest As New Bitmap( _
            CInt(bm_source.Width * scale_factor), _
            CInt(bm_source.Height * scale_factor))

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bm_source, 0, 0, _
            bm_dest.Width + 1, _
            bm_dest.Height + 1)
        ' Display the result.
        PictureBox4.Image = bm_dest


As you can see the image it's scaled to a lower resolution and when i finish drawing on it and click the save button , it re-sizes the bitmap to the original's image
VB
Using Bmp As New Bitmap(PictureBox4.Image, defaultWidth, defaultHeight)

resolution and saves the image . But the image is saved with a blur over it and each time i re-save that image it keeps adding more blur to it. How could i prevent the blur from applying on the saved image.Thank you !
Posted
Updated 25-Jun-14 12:26pm
v2

Start here:
Bmp.Save(str1 & "\" & str2, Imaging.ImageFormat.Jpeg)

Don't use Imaging.ImageFormat.Jpeg - it's a lossy compression technique and that means that it "throws away" information ion order to save space every single time you save it. The chance are, this is the source of your "blurriness".

Try it: load a BMP, save it as JPG.
Load the JPG, save it again.
Repeat a few times and you end up with a remarkably small file, that looks very little like the original without needing to make any changes to it - and with no chnace whatsoever of getting the sharpness back, despite what you see on CSI...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jun-14 13:31pm    
Good point, my 5.
But look thoroughly what OP is doing. The whole thing is totally pointless. (See my answer.)
—SA
In addition to Solution 1: some of the blur can also occur during rendering, which can be cured by using the best-quiality interpolation mode and other rendering options.

But this is not your biggest problem. The whole piece of code is totally pointless. Look what you are doing: you initialize bmp using PictureBox4.Image. But why not using PictureBox4.Image itself? This is a redundant operations.

Now, look what you do next. On the new bitmap bmp you draw… the save very bitmap! with possible loss of quality! one more redundant operation!

I'm not sure you have any idea what you are doing and what do you want to do.

—SA
 
Share this answer
 
Thank you OriginalGriff , i have tried your way and with no result from what i have read on google this blur appears due to the bitmap resize .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900