2010-01-27 9 views
10

में छवियों का आकार बदलना मैं vb.net का उपयोग कर छवियों का आकार बदलने के लिए एक साधारण वीबी उपयोगिता बनाना चाहता हूं। मुझे वास्तव में छवियों में हेरफेर करने के लिए उपयोग करने के लिए वीबी कक्षा का उपयोग करने में परेशानी हो रही है। छवि वर्ग और बिटमैप वर्ग काम नहीं करते हैं।वीबीएनईटी

कोई भी विचार, संकेत, टिप्स, ट्यूटोरियल लिंक की बहुत सराहना की जाती है।

धन्यवाद।

उत्तर

13

Here is an article यह कैसे करें इस पर पूर्ण विवरण के साथ।

Private Sub btnScale_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles btnScale.Click 
    ' Get the scale factor. 
    Dim scale_factor As Single = Single.Parse(txtScale.Text) 

    ' Get the source bitmap. 
    Dim bm_source As New Bitmap(picSource.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. 
    picDest.Image = bm_dest 
End Sub 

[संपादित करें]
समान तर्ज पर One more

2

ज्यादा VB.NET वाक्य रचना पता नहीं है, लेकिन यहाँ और विचार

Dim source As New Bitmap("C:\image.png") 
Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb) 

Using graphics As Graphics = Graphics.FromImage(target) 
    graphics.DrawImage(source, new Size(48, 48)) 
End Using 
4

यह पुन: आकार किसी भी छवि अल्फा के साथ 32bpp लिए समर्थन के साथ अच्छी गुणवत्ता का उपयोग कर रहा है। नई छवि में मूल पहलू अनुपात में नए के अंदर केंद्रित मूल छवि होगी।

#Region " ResizeImage " 
    Public Overloads Shared Function ResizeImage(SourceImage As Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap 
     Dim bmSource = New Drawing.Bitmap(SourceImage) 

     Return ResizeImage(bmSource, TargetWidth, TargetHeight) 
    End Function 

    Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap 
     Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight, Drawing.Imaging.PixelFormat.Format32bppArgb) 

     Dim nSourceAspectRatio = bmSource.Width/bmSource.Height 
     Dim nDestAspectRatio = bmDest.Width/bmDest.Height 

     Dim NewX = 0 
     Dim NewY = 0 
     Dim NewWidth = bmDest.Width 
     Dim NewHeight = bmDest.Height 

     If nDestAspectRatio = nSourceAspectRatio Then 
      'same ratio 
     ElseIf nDestAspectRatio > nSourceAspectRatio Then 
      'Source is taller 
      NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio * NewHeight)) 
      NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth)/2)) 
     Else 
      'Source is wider 
      NewHeight = Convert.ToInt32(Math.Floor((1/nSourceAspectRatio) * NewWidth)) 
      NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight)/2)) 
     End If 

     Using grDest = Drawing.Graphics.FromImage(bmDest) 
      With grDest 
       .CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality 
       .InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic 
       .PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality 
       .SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias 
       .CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver 

       .DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight) 
      End With 
     End Using 

     Return bmDest 
    End Function 
#End Region 
+0

'Drawing2D.SmoothingMode' यहां लागू नहीं होता है, यह 2 डी वेक्टर ड्राइंग के लिए ही प्रासंगिक है 'Graphics.DrawLine' – alldayremix

+0

जैसी विधियां इस से सावधान रहें। 'GrDest' सेक्शन' में अल्फा मानों को कभी-कभी थोड़ा बढ़ाया गया, केवल छवि के अर्द्ध-अपारदर्शी तत्व के साथ एक ही छवि के पुनरावर्तक प्रसंस्करण पर ध्यान देने योग्य। समय के साथ, यह कम और कम अपारदर्शी बन गया। मैंने SourceMopy पर .SoothingMode भाग और परिवर्तित कंपोजिटिंग मोड को टिप्पणी की। अभी भी परीक्षण है लेकिन इन दोनों में से एक ने ऐसा किया है। डर है कि मैं एक सटीक उत्तर नहीं दे सकता क्योंकि जीडीआई को अच्छी तरह से समझ में नहीं आता है। हो सकता है कि @ सिटीटर जीडीआई के बारे में अपने ज्ञान को आगे बढ़ाने में मदद कर सके। – stigzler

12

आप बस Visual Basic .NET में आपकी छवि का आकार करने के लिए इस एक पंक्ति कोड का उपयोग कर सकते

Public Shared Function ResizeImage(ByVal InputImage As Image) As Image 
     Return New Bitmap(InputImage, New Size(64, 64)) 
End Function 

कहाँ;

  1. "इनपुट इमेज" वह छवि है जिसे आप आकार बदलना चाहते हैं।
  2. "64 एक्स 64" आवश्यक आकार आप इसे बदल सकते हैं अपनी आवश्यकताओं यानी 32x32 आदि के रूप में है
0
Dim x As Integer = 0 
    Dim y As Integer = 0 
    Dim k = 0 
    Dim l = 0 
    Dim bm As New Bitmap(p1.Image) 
    Dim om As New Bitmap(p1.Image.Width, p1.Image.Height) 
    Dim r, g, b As Byte 
    Do While x < bm.Width - 1 
     y = 0 
     l = 0 
     Do While y < bm.Height - 1 
      r = 255 - bm.GetPixel(x, y).R 
      g = 255 - bm.GetPixel(x, y).G 
      b = 255 - bm.GetPixel(x, y).B 
      om.SetPixel(k, l, Color.FromArgb(r, g, b)) 
      y += 3 
      l += 1 
     Loop 
     x += 3 
     k += 1 
    Loop 
    p2.Image = om