2008-10-09 12 views
10

मैं एक वेब पेज के भीतर प्रदर्शित करने के लिए एक .aspx पृष्ठ से एक पारदर्शी जीआईएफ वापस करने की कोशिश कर रहा हूं। मैं छवि को पारदर्शिता प्राप्त करने की कोशिश कर रहा हूं, लेकिन मैं सिर्फ ब्लैक होने जा रहा हूं जहां छवि पारदर्शी होनी चाहिए।आप सिस्टम का उपयोग कर पारदर्शी छवि कैसे आकर्षित करते हैं। ड्रॉइंग?

किसी को भी मैं गलत क्या कर रहा पता है?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles Me.Load 
    '' Change the response headers to output a GIF image. 
    Response.Clear() 
    Response.ContentType = "image/gif" 

    Dim width = 110 
    Dim height = width 

    '' Create a new 32-bit bitmap image 
    Dim b = New Bitmap(width, height) 

    '' Create Grahpics object for drawing 
    Dim g = Graphics.FromImage(b) 

    Dim rect = New Rectangle(0, 0, width - 1, height - 1) 

    '' Fill in with Transparent 
    Dim tbrush = New System.Drawing.SolidBrush(Color.Transparent) 
    g.FillRectangle(tbrush, rect) 

    '' Draw Circle Border 
    Dim bPen = Pens.Red 
    g.DrawPie(bPen, rect, 0, 365) 

    '' Fill in Circle 
    Dim cbrush = New SolidBrush(Color.LightBlue) 
    g.FillPie(cbrush, rect, 0, 365) 


    '' Clean up 
    g.Flush() 
    g.Dispose() 

    '' Make Transparent 
    b.MakeTransparent() 

    b.Save(Response.OutputStream, Imaging.ImageFormat.Gif) 
    Response.Flush() 
    Response.End() 
End Sub 
+0

मैंने अपनी पोस्ट हटा दी है, इसलिए यह इसे अव्यवस्थित नहीं करता है। – mattlant

उत्तर

5

दुर्भाग्य से, बिटमैप ऑब्जेक्ट का उपयोग करके पारदर्शी GIF बनाने का कोई आसान तरीका नहीं है। (this KB article देखें)

आप वैकल्पिक रूप से पीएनजी प्रारूप का उपयोग कर सकते हैं जो आपके द्वारा उपयोग किए जा रहे कोड के साथ पारदर्शिता का समर्थन करता है।

6

हां, जैसा कि जेरोम ने कहा है, बिटकमैप ऑब्जेक्ट का उपयोग करके पारदर्शी जीआईएफ बनाने के लिए वैसे भी नहीं है। बकवास!

ठीक है, वैसे भी, मैंने एक पीएनजी उत्पन्न करने के लिए अपना कोड बदल दिया और अपेक्षित सभी कार्यों को बदल दिया।

मुझे एक छोटा सा काम करना है क्योंकि मुझे पीएनजी को सीधे आउटपुटस्ट्रीम में नहीं लिखना है। मुझे पीएनजी को मेमोरीस्ट्रीम में लिखना था, और उसके बाद आउटपुटस्ट्रीम को लिखना था।

यहाँ मेरी कार्यान्वयन के लिए अंतिम कोड है:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles Me.Load 
    '' Change the response headers to output a JPEG image. 
    Response.Clear() 
    Response.ContentType = "image/png" 

    Dim width = 11 
    Dim height = width 

    '' Create a new 32-bit bitmap image 
    Dim b = New Bitmap(width, height) 

    '' Create Grahpics object for drawing 
    Dim g = Graphics.FromImage(b) 

    '' Fill the image with a color to be made Transparent after drawing is finished. 
    g.Clear(Color.Gray) 

    '' Get rectangle where the Circle will be drawn 
    Dim rect = New Rectangle(0, 0, width - 1, height - 1) 

    '' Draw Circle Border 
    Dim bPen = Pens.Black 
    g.DrawPie(bPen, rect, 0, 365) 

    '' Fill in Circle 
    Dim cbrush = New SolidBrush(Color.Red) 
    g.FillPie(cbrush, rect, 0, 365) 

    '' Clean up 
    g.Flush() 
    g.Dispose() 

    '' Make Transparent 
    b.MakeTransparent(Color.Gray) 

    '' Write PNG to Memory Stream then write to OutputStream 
    Dim ms = New MemoryStream() 
    b.Save(ms, Imaging.ImageFormat.Png) 
    ms.WriteTo(Response.OutputStream) 

    Response.Flush() 
    Response.End() 
End Sub 
4

यह संभव है, लेकिन आसान नहीं
आप अपने प्रोजेक्ट में असुरक्षित कोड का उपयोग करने में सक्षम हैं, वहाँ करने के लिए कुछ तरीके हैं कलर टेबल के माध्यम से चीरने और पारदर्शिता कार्य करने के लिए पॉइंटर्स का उपयोग करें।

बॉब पॉवेल द्वारा नमूना फॉर्म ऐप http://www.bobpowell.net/giftransparency.htm पर उपलब्ध है। मैंने कुछ साल पहले वेब हैंडलर पर इस विधि पर एक भिन्नता का उपयोग किया था जो कि महीने में लगभग 10 मिलियन बार हिट हो रहा था, और यह ठीक काम करने लग रहा था।

यदि आप केवल सीमित रंगीन पैलेट का उपयोग कर रहे हैं तो आप केवल रंगीन रंगों को रंग तालिका प्रसंस्करण को कम कर सकते हैं (मुझे याद नहीं है कि मैंने यह कैसे किया ...)।

कहा जा रहा है कि, पीएनजी एक मीट्रिक crapload आसान है।

2

यहाँ एक gif तब्दील (जो पहले से ही उस में पारदर्शिता नहीं है) के लिए कुछ कोड है (माना जाता आप उसका आकार बदल करना चाहते हैं) बिटमैप में और उसके बाद पता चला जा सकता है ठीक से साथ यह पारदर्शिता है।

imagePath = System.Web.HttpContext.Current.Request.MapPath(libraryPath + reqImageFile); 
System.Drawing.Image image = null; 
Bitmap resizedImage = null; 

if (reqWidth == 0) { reqWidth = image.Width; } 
if (reqHeight == 0) { reqHeight = image.Height; } 
image = System.Drawing.Image.FromFile(imagePath); 
reqWidth = image.Width; 
reqHeight = image.Height; 

//here is the transparency 'special' treatment 
resizedImage = new Bitmap(reqWidth, reqHeight, PixelFormat.Format8bppIndexed); 
ColorPalette pal = resizedImage.Palette; 
for (int i = 0; i < pal.Entries.Length; i++) 
{ 
     Color col = pal.Entries[i]; 
     pal.Entries[i] = Color.FromArgb(0, col.R, col.G, col.B); 
} 
resizedImage.Palette = pal; 
BitmapData src = ((Bitmap)image).LockBits(new Rectangle(0, 0, reqWidth, reqHeight), ImageLockMode.ReadOnly, image.PixelFormat); 
BitmapData dst = resizedImage.LockBits(new Rectangle(0, 0, resizedImage.Width, resizedImage.Height), 
ImageLockMode.WriteOnly, resizedImage.PixelFormat); 
((Bitmap)image).UnlockBits(src); 
resizedImage.UnlockBits(dst); 

शुभकामनाएँ!

ग्रेगोयर लाफोर्ट्यून

संबंधित मुद्दे