ASP.NET에서 워터마크를 만들기 위해 이미지에 다른 이미지를 스탬프하는 가장 좋은 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/110018

문제

아는 사람 있나요?이미지에 다른 이미지를 워터마크로 즉각적으로 스탬프를 찍을 수 있고 대규모 일괄 작업도 수행할 수 있기를 원합니다.어떤 유형의 기존 라이브러리나 알고 있는 기술이라도 좋습니다.

도움이 되었습니까?

해결책

귀하의 질문에 대한 답변은 다음과 같습니다.

http://www.codeproject.com/KB/GDI-plus/watermark.aspx

행운을 빌어요!

다른 팁

나는 행운을 누렸다 이미지매직.그것은 .NET용 API 도.

내 전체 기사는 다음과 같습니다. http://forums.asp.net/p/1323176/2634923.aspx

SDK 명령 프롬프트를 사용하여 활성 폴더에서 아래 소스 코드가 포함된 폴더로 이동합니다.그런 다음 다음을 사용하여 코드를 컴파일하십시오.


vbc.exe watermark.vb /t:exe /out:watermark.exe

그러면 폴더에 exe가 생성됩니다.exe는 두 가지 매개변수를 허용합니다.전.


watermark.exe "c:\source folder" "c:\destination folder"

이는 상위 폴더를 통해 반복되며 모두 하위 폴더.발견된 모든 JPEG는 코드에 지정한 이미지로 워터마크가 표시되고 대상 폴더에 복사됩니다.원본 이미지는 그대로 유지됩니다.

// watermark.vb --


Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO

Namespace WatermarkManager
    Class Watermark
        Shared sourceDirectory As String = "", destinationDirectory As String = ""

        Overloads Shared Sub Main(ByVal args() As String)

            'See if an argument was passed from the command line
            If args.Length = 2 Then
                sourceDirectory = args(0)
                destinationDirectory = args(1)

                ' make sure sourceFolder is legit
                If Directory.Exists(sourceDirectory) = False
                    TerminateExe("Invalid source folder. Folder does not exist.")
                    Exit Sub
                End If

                ' try and create destination folder
                Try
                    Directory.CreateDirectory(destinationDirectory)
                Catch
                    TerminateExe("Error creating destination folder. Invalid path cannot be created.")
                    Exit Sub
                End Try

                ' start the magic
                CreateHierarchy(sourceDirectory,destinationDirectory)

            ElseIf args.Length = 1
                If args(0) = "/?"
                    DisplayHelp()
                Else
                    TerminateExe("expected: watermark.exe [source path] [destination path]")
                End If
                Exit Sub
            Else
                TerminateExe("expected: watermark.exe [source path] [destination path]")
                Exit Sub
            End If

            TerminateExe()
        End Sub

        Shared Sub CreateHierarchy(ByVal sourceDirectory As String, ByVal destinationDirectory As String)

            Dim tmpSourceDirectory As String = sourceDirectory

            ' copy directory hierarchy to destination folder
            For Each Item As String In Directory.GetDirectories(sourceDirectory)
                Directory.CreateDirectory(destinationDirectory + Item.SubString(Item.LastIndexOf("\")))

                If hasSubDirectories(Item)
                    CreateSubDirectories(Item)
                End If
            Next

            ' reset destinationDirectory
            destinationDirectory = tmpSourceDirectory

            ' now that folder structure is set up, let's iterate through files
            For Each Item As String In Directory.GetDirectories(sourceDirectory)
                SearchDirectory(Item)
            Next
        End Sub

        Shared Function hasSubDirectories(ByVal path As String) As Boolean
            Dim subdirs() As String = Directory.GetDirectories(path)
            If subdirs.Length > 0
                Return True
            End If
            Return False
        End Function

        Shared Sub CheckFiles(ByVal path As String)
            For Each f As String In Directory.GetFiles(path)
                If f.SubString(f.Length-3).ToLower = "jpg"
                    WatermarkImage(f)
                End If
            Next
        End Sub

        Shared Sub WatermarkImage(ByVal f As String)

            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(f)
            Dim graphic As Graphics
            Dim indexedImage As New Bitmap(img)
            graphic = Graphics.FromImage(indexedImage)
            graphic.DrawImage(img, 0, 0, img.Width, img.Height)
            img = indexedImage

            graphic.SmoothingMode = SmoothingMode.AntiAlias
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic

            Dim x As Integer, y As Integer
            Dim source As New Bitmap("c:\watermark.png")
            Dim logo As New Bitmap(source, CInt(img.Width / 3), CInt(img.Width / 3))
            source.Dispose()
            x = img.Width - logo.Width
            y = img.Height - logo.Height
            graphic.DrawImage(logo, New Point(x,y))
            logo.Dispose()

            img.Save(destinationDirectory+f.SubString(f.LastIndexOf("\")), ImageFormat.Jpeg)
            indexedImage.Dispose()
            img.Dispose()
            graphic.Dispose()

            Console.WriteLine("successfully watermarked " + f.SubString(f.LastIndexOf("\")+1))
            Console.WriteLine("saved to: " + vbCrLf + destinationDirectory + vbCrLf)

        End Sub

        Shared Sub SearchDirectory(ByVal path As String)
            destinationDirectory = destinationDirectory + path.SubString(path.LastIndexOf("\"))
            CheckFiles(path)
            For Each Item As String In Directory.GetDirectories(path)
                destinationDirectory += Item.SubString(Item.LastIndexOf("\"))

                CheckFiles(Item)

                If hasSubDirectories(Item)
                    destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
                    SearchDirectory(Item)
                    destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
                End If
                destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
            Next
            destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
        End Sub

        Shared Sub CreateSubDirectories(ByVal path As String)
            destinationDirectory = destinationDirectory + path.SubString(path.LastIndexOf("\"))
            For Each Item As String In Directory.GetDirectories(path)
                destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
                Directory.CreateDirectory(destinationDirectory)
                Console.WriteLine(vbCrlf + "created: " + vbCrlf + destinationDirectory)

                If hasSubDirectories(Item)
                    destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
                    CreateSubDirectories(Item)
                    destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
                End If
                destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
            Next
            destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
        End Sub

        Shared Sub TerminateExe(ByVal Optional msg As String = "")
            If msg  ""
                Console.WriteLine(vbCrLf + "AN ERROR HAS OCCURRED //" + vbCrLf + msg)
            End If
            Console.WriteLine(vbCrLf + "Press [enter] to close...")
            'Console.Read()
        End Sub

        Shared Sub DisplayHelp()
            Console.WriteLine("watermark.exe accepts two parameters:" + vbCrLf + " - [source folder]")
            Console.WriteLine(" - [destination folder]")
            Console.WriteLine("ex." + vbCrLf + "watermark.exe ""c:\web_projects\dclr source"" ""d:\new_dclr\copy1 dest""")
            Console.WriteLine(vbCrLf + "Press [enter] to close...")
            Console.Read()
        End Sub
    End Class
End Namespace
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top