मेरे पास एक छवि है (.png प्रारूप में), और मैं चाहता हूं कि यह तस्वीर बाइनरी में परिवर्तित हो।छवि को बाइनरी में कनवर्ट करें?
सी # का उपयोग करके यह कैसे किया जा सकता है?
मेरे पास एक छवि है (.png प्रारूप में), और मैं चाहता हूं कि यह तस्वीर बाइनरी में परिवर्तित हो।छवि को बाइनरी में कनवर्ट करें?
सी # का उपयोग करके यह कैसे किया जा सकता है?
चूंकि आप एक फ़ाइल का उपयोग किया है: -
Response.ContentType = "image/png";
Response.WriteFile(physicalPathOfPngFile);
का उपयोग करके इसे संभाल सकते हैं इसे वापस कैसे परिवर्तित करें छवि? –
byte[] b = File.ReadAllBytes(file);
एक बाइनरी फ़ाइल खोलता है, एक बाइट सरणी में फ़ाइल की सामग्री पढ़ता है, और फिर फ़ाइल को बंद कर देता है।
के बारे में सोच रहे हैं, एक अलग प्रारूप है, जब तक कि प्रतिक्रिया में भेजने से पहले उस सरणी को संसाधित करने की आवश्यकता न हो, आप एएसपी.NET को WriteFile – AnthonyWJones
इस प्रयास करें:
Byte[] result
= (Byte[])new ImageConverter().ConvertTo(yourImage, typeof(Byte[]));
आप कर सकता है:
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
BinaryReader streamreader = new BinaryReader(stream);
byte[] data = streamreader.ReadBytes(stream.Length);
डेटा तो की सामग्री होते हैं छवि।
सबसे पहले, छवि को ImageConverter कक्षा का उपयोग करके एक बाइट सरणी में परिवर्तित करें। फिर अपनी पीएनजी छवि के mime type और वॉयला निर्दिष्ट करें!
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Byte[]));
Response.ContentType = "image/png";
Response.BinaryWrite((Byte[])tc.ConvertTo(img,tc));
System.Drawing.Image image = System.Drawing.Image.FromFile("filename");
byte[] buffer;
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
buffer = stream.ToArray(); // converted to byte array
stream = new MemoryStream();
stream.Read(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
हाँ, धन्यवाद। ठीक किया! –
public static byte[] ImageToBinary(string imagePath)
{
FileStream fS = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
byte[] b = new byte[fS.Length];
fS.Read(b, 0, (int)fS.Length);
fS.Close();
return b;
}
सिर्फ कोड ऊपर का उपयोग मुझे लगता है कि आपकी समस्या को हल किया जा जाएगा
using System.IO;
FileStream fs=new FileStream(Path, FileMode.Open, FileAccess.Read); //Path is image location
Byte[] bindata= new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
क्या आप मतलब है 'बाइनरी में बदलने का':
यहाँ एक उदाहरण है? क्या आपका मतलब है, उदाहरण के लिए, काला और सफेद? – pavium
क्या आप इसे थोड़ा और समझा सकते हैं? एक छवि पहले से ही बाइनरी है। क्या आप इसे डिकंप्रेस करना चाहते हैं, क्या आप पिक्सेल तक पहुंच बनाना चाहते हैं? –
मुझे Response.BinaryWrite() का उपयोग कर स्क्रीन पर छवि के बाइनरी डेटा को लिखना है; – Martijn