문제
안녕하세요 감사합니다.이 코드에는 아래에 하나의 코드가 있습니다. 하나의 이미지를 업로드합니다. 바이트 코드로 변환하고 데이터베이스에 저장하고 GridView에 검색하십시오. .. ...
protected void btnUpload_Click(object sender, EventArgs e)
{
string strID= txtid.Text.ToString();
string strImageName = txtName.Text.ToString();
if (FileUpload1.PostedFile != null &&
FileUpload1.PostedFile.FileName != "")
{
byte[] imageSize = new byte
[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read
(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
// Create SQL Connection
SqlConnection con = new SqlConnection("user id=sa;password=Zoomin@123;database=salary_db;server=192.168.1.100");
// Create SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO image1(ID,ImageName,Image)" +
" VALUES (@ID,@ImageName,@Image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter ID = new SqlParameter
("@ID", SqlDbType.VarChar, 50);
ID.Value = strID.ToString();
cmd.Parameters.Add(ID);
SqlParameter ImageName = new SqlParameter
("@ImageName", SqlDbType.VarChar, 50);
ImageName.Value = strImageName.ToString();
cmd.Parameters.Add(ImageName);
SqlParameter UploadedImage = new SqlParameter
("@Image", SqlDbType.Image, imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblMessage.Text = "File Uploaded";
GridView1.DataBind();
}}
해결책
다음 기능을 사용할 수 있습니다.
public void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)
{
using (var image = Image.FromStream(fromStream))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
using (var thumbnailBitmap = new Bitmap(newWidth, newHeight))
using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
{
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save(toStream, image.RawFormat);
}
}
}
매개 변수의 이름은 매우 자명해야합니다.
다른 팁
살펴보십시오
제휴하지 않습니다 StackOverflow