将带有CMYK颜色空间的JPG转换为.NET中的RGB

时间:2014-04-16 15:03:55

标签: .net vb.net image colors

嗯,我认为标题大多是这样说的。我想上传可能有各种格式的图片。一般来说,我想按原样保存。但如果它的JPG具有CMYK色彩空间,我想在保存时将色彩空间转换为RGB。在VB.NET中有一种简单的方法吗? (或者很难?)

1 个答案:

答案 0 :(得分:0)

这是一个简单的递归 CMYK到RGB JPEG转换器:

<%@ OutputCache Duration="600" VaryByParam="*" %>
<%@ Page Debug="false" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>

<script language="VB" runat="server">

' CMYK to RGB image converter v1.0

Dim MyFolder as String = "PHOTO"

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    response.write ("<!DOCTYPE html><HTML><HEAD><TITLE>CMYK to RGB converter</TITLE><STYLE>HTML {margin:0px}BODY {font-family:Arial,Helvetica,SansSerif;margin:10px}</STYLE></HEAD><BODY><H1>CMYK to RGB converter</H1><H3>v1.0 by TFI</H3><BR>Image folder: <b>"+MyFolder+"</b><BR><BR>")
    Dim dir As String = Server.MapPath(".")
    Dim folders() As String = Directory.GetDirectories(dir)     
    for each folder as String in folders        
        if mid(folder,InStrRev(folder,"\")+1)=MyFolder then
            Dim files() As String = Directory.GetFiles(folder,"*.jpg",SearchOption.AllDirectories)
            if ubound(files)>=0 then
                for each file as String in files
                    Response.write ("Converting "+file)
                    Convert(file)
                    Response.Flush()
                next
            else
                response.write ("No images found.")
            end if
        end if  
    Next
    response.write ("</BODY></HTML>")
End Sub 

Sub Convert(imageName as String)
    Dim imgFullSizeTemp As System.Drawing.Image
    imgFullSizeTemp = System.Drawing.Image.FromFile(imageName)      
    If imgFullSizeTemp Is Nothing Then Response.End()   
    Dim imgFullSize as new Bitmap(imgFullSizeTemp.Width, imgFullSizeTemp.Height, PixelFormat.Format24bppRgb)
    Dim g As Graphics = Graphics.FromImage(imgFullSize)         
    g.DrawImage(imgFullSizeTemp, 0, 0, imgFullSizeTemp.Width, imgFullSizeTemp.Height)   
    g.Dispose()
    imgFullSizeTemp.Dispose()
    response.write (" >>> Converted!<BR><BR>")
    Dim clone as new Bitmap(imgFullSize)
    clone.Save(imageName, ImageFormat.Jpeg)
End Sub

</script>