Results 1 to 10 of 10

Thread: Problem with zooming on a picture

  1. #1

    Thread Starter
    Addicted Member javad2000's Avatar
    Join Date
    Dec 2006
    Posts
    238

    Problem with zooming on a picture

    Hello

    I have a very small black&white picture (4*4 pixels). Here's a magnified view of the picture:


    I want to show this picture in a PictureBox whose "SizeMode" property is set to "Stretched". Normally, I expect the image to be larger than the source image and show something like this:


    But it shows this:


    How can I solve this problem?

    Thank u

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Problem with zooming on a picture

    save 1 of the larger pictures from this page + use that instead.
    a larger picture made larger will probably display properly.

  3. #3

    Thread Starter
    Addicted Member javad2000's Avatar
    Join Date
    Dec 2006
    Posts
    238

    Re: Problem with zooming on a picture

    Quote Originally Posted by .paul.
    save 1 of the larger pictures from this page + use that instead.
    a larger picture made larger will probably display properly.
    Thank u Paul
    Let me ask my Q another way:

    I need to magnify B&W pictures in B&W mode, not in color mode. I want to make all the pictures in that picturebox 'Black & white". How is it possible?

    Thanks

  4. #4
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Problem with zooming on a picture

    I have an idea how to do this. I'll try writing up a function for you. Hold on.

  5. #5
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Problem with zooming on a picture

    First of all, when using the small image in the PictureBox.Image property, and setting the SizeMode to StretchImage, the image shows up exactly how you wanted it.

    Secondly, your original JPEG image has speckles on it. This is probably due to the fact that it lost formatting when you saved it as a JPEG. Perhaps you should try saving the original, perfectly formatted image in Photoshop to avoid formatting loss.

    If the SizeMode property isn't working for you, which it should, I wrote up this function anyways to resize an image. Maybe it will work better for you.

    vb.net Code:
    1. Public Shared Function ResizeImage(ByVal sourcePath As String, ByRef newSize As Size)
    2.     Dim img As Image = Image.FromFile(sourcePath)
    3.     Dim imgNew As New Bitmap(newSize.Width, newSize.Height, Imaging.PixelFormat.Format1bppIndexed)
    4.     Dim graphics As Graphics = graphics.FromImage(imgNew)
    5.  
    6.     graphics.DrawImage(img, 0, 0, imgNew.Width, imgNew.Height)
    7.  
    8.     Return imgNew
    9. End Function

    vb.net Code:
    1. PictureBox1.Image = ResizeImage("C:\Documents and Settings\Fromethius\Desktop\image.gif", New Size(104, 107))

    If you cannot create a properly formatted image, you can always make an image black and white and then apply resizing, as that should work.

    You can make an image black and white using a properly coded ColorMatrix which should look something like this:

    vb.net Code:
    1. Dim cm As New ColorMatrix(New Single()() {New Single() {0.5F, 0.5F, 0.5F, 0, 0}, New Single() {0.5F, 0.5F, 0.5F, 0, 0}, New Single() {0.5F, 0.5F, 0.5F, 0, 0}, New Single() {0, 0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 0, 1}})

    Then you can use the ImageAttributes class to create a Bitmap like this.

    Also, you can try looking into the Graphics.InterpolationMode for proper resizing.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Problem with zooming on a picture

    fromethius, did you try that with a 4*4 image as the original?

    i'm not sure what you mean with the colormatrix thing. how are you making the image b+w?

  7. #7
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Problem with zooming on a picture

    No, I didn't yet because she didn't post it. I only tried this one:

    http://aycu23.webshots.com/image/400...2488927_rs.jpg

    About the ColorMatrix: You can make an image monochrome by using a ColorMatrix and ImageAttributes.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Problem with zooming on a picture

    can you give an example?

  9. #9
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Problem with zooming on a picture

    Yea, sure. Here you go. I just wrote this up:

    vb.net Code:
    1. Imports System.Drawing.Imaging
    2.  
    3. Public Class Form1
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         PictureBox1.Image = GetGreyscaledImage(New Bitmap("C:\Documents and Settings\Fromethius\My Documents\My Pictures\image.png"))
    6.     End Sub
    7.  
    8.     Public Shared Function GetGreyscaledImage(ByRef setImage As Image) As Image
    9.         Dim greyscaleBitmap As Bitmap = New Bitmap(setImage.Width, setImage.Height)
    10.         Dim greyscaleGraphics As Graphics = Graphics.FromImage(greyscaleBitmap)
    11.  
    12.         Dim greyscaleColorMatrix As New ColorMatrix(New Single()() { _
    13.             New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
    14.             New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
    15.             New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
    16.             New Single() {0, 0, 0, 1, 0, 0}, _
    17.             New Single() {0, 0, 0, 0, 1, 0}, _
    18.             New Single() {0, 0, 0, 0, 0, 1}})
    19.  
    20.         Dim greyscaleImageAttributes As New ImageAttributes()
    21.         greyscaleImageAttributes.SetColorMatrix(greyscaleColorMatrix)
    22.  
    23.         greyscaleGraphics.DrawImage(setImage, New Rectangle(0, 0, setImage.Width, setImage.Height), 0, 0, setImage.Width, setImage.Height, GraphicsUnit.Pixel, greyscaleImageAttributes)
    24.  
    25.         Return greyscaleBitmap
    26.     End Function
    27. End Class

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Problem with zooming on a picture

    ok thanks that works.
    i'd rate you but i haven't rated 10 people since last time....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width