كيف يمكنني تحويل الصور النقطية إلى البايت []؟
-
06-07-2019 - |
سؤال
وأساسا أنا إدراج صورة باستخدام listviews إدخال الحدث، في محاولة لتغيير صورة من السيطرة fileupload، ومن ثم حفظها في قاعدة بيانات SQL باستخدام LINQ.
ولقد وجدت بعض التعليمات البرمجية لإنشاء صورة نقطية جديدة من المحتوى في السيطرة fileupload، ولكن هذا لتخزينه في ملف على الخادم، من <لأ href = "http://forums.asp.net/t /1208353.aspx "يختلط =" noreferrer "> هذا المصدر ، ولكني في حاجة إلى حفظ الصورة النقطية مرة أخرى إلى قاعدة بيانات SQL، وأنا أعتقد أنني في حاجة لتحويل مرة أخرى إلى بايت [] شكل.
وإذا كيف يمكنني تحويل الصورة النقطية إلى بايت [] شكل؟
إذا أنا ذاهب عن هذا بطريقة خاطئة وسأكون ممتنا أنه يمكن تصحيح لي.
وهنا هو رمز بلادي:
// Find the fileUpload control
string filename = uplImage.FileName;
// Create a bitmap in memory of the content of the fileUpload control
Bitmap originalBMP = new Bitmap(uplImage.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 100;
int newHeight = sngRatio * newWidth;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
PHJamesDataContext db = new PHJamesDataContext();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
PHJProjectPhoto myPhoto =
new PHJProjectPhoto
{
ProjectPhoto = data,
OrderDate = DateTime.Now,
ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
ProjectId = selectedProjectId
};
db.PHJProjectPhotos.InsertOnSubmit(myPhoto);
db.SubmitChanges();
المحلول
ويجب أن تكون قادرة على تغيير هذه الكتلة إلى
System.IO.MemoryStream stream = new System.IO.MemoryStream();
newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
PHJProjectPhoto myPhoto =
new PHJProjectPhoto
{
ProjectPhoto = stream.ToArray(), // <<--- This will convert your stream to a byte[]
OrderDate = DateTime.Now,
ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
ProjectId = selectedProjectId
};
نصائح أخرى
إذا كنت قد حصلت على MemoryStream
بالفعل، مجرد دعوة <لأ href = "http://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray.aspx" يختلط = "noreferrer" > MemoryStream.ToArray
للحصول على البيانات بها.
وعلى افتراض أن الصورة النقطية الخاصة بك هي بمب
byte[] data;
using(System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
stream.Close();
}